Solved

How do I strip characters from a string that is read in through a card reader? (swipe)

Posted on 2013-02-06
23
421 Views
Last Modified: 2013-04-10
I currently have a piece of java script (jQuery) that strips characters from a string as they are swiped in:

                        $(".studentIdInput").keyup(function () {
                            var value = jQuery(this).val();
                            var rSt = '';
                            var lSt = '';
                            if (value.length == 17) {
                                rSt = value.substring(8);
                                lSt = rSt.slice(0, -2);
                                $(".studentIdInput").val(lSt);
                            }
                        });

this works fine when i'm looking for the characters from the right of the string. how do i strip the characters in the following string that i need, from the left? I need, starting with (and including) the 2nd character and the following 7 characters after it, so 8 total characters. Here's the string:

%ABC12345^BLAH/BLAH?;1111111111111111=2222?

So I would need, the substring: ABC12345.

The characters that I need are between the % and the ^, so is there something I can do in jQuery or javascript that will do this for me? I think my other code worked because it pulled from the right side of the string. I now need it to pull from the left, after the swipe is complete.
0
Comment
Question by:mikesExpertExchange
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 2
23 Comments
 
LVL 1
ID: 38862006
use:

rSt = value.substring(1,8);

to get characters 2-9.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38862014
@thisstupidservicewontletmeusemyname: will this work with any length string? in other words if i removed the if logic, will it still work?

if (value.length == 17)
0
 
LVL 1
ID: 38862094
It will work with any length string. Use substring(1,8) for characters 2-9, substring(4,10) for characters 5-11 etc.
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38862201
When I took that conditional out, I got nothing. And, does substring pull from the left or the right? I need it to pul starting on the left end of the string since the length of the string will vary. Also, with no conditional it attempts to strip the first thing (anything) that i type into the textbox.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38863413
You can use Min(8, string.length) as 2nd argumento of the function instead.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38863665
@Sar1973: So do you mean like this? Or which function are you talking about? Substring or Slice?

                        $(".studentIdInput").keyup(function () {
                            var value = jQuery(this).val();
                            var rSt = '';
                            var lSt = '';
                            if (value.length == 17) {
                                rSt = value.substring(8,  Min(8, string.length));
                                lSt = rSt.slice(0, -2);
                                $(".studentIdInput").val(lSt);
                            }
                        });
 And again can i leave out the if (value.length == ??) condition? that seems to be causing more of a problem with the actual capturing of what is swiped in. Since I'm doin this in the .keyUp event, i think I have to have that condition in there. otherwise each time a "keyUp" event is detected, it will attempt this function and if there is only 1 character in that box then it doesn't behave as desired.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38863687
The if statement is no longer necessary, if you know that your string will have a length between 1 and N characters. But notice that I typed string.length using "string" instead of the name of your string (is it "studentIdInput"?).
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38863960
@Sar1973: I know that the length of my string will vary. this script is set to run against any controls (in this case, a text box), with the class .studentIdInput. so when you say: "string.length" I think, the equivalent would be: "value.length" in this case, do you agree?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38864031
In JS it would be something like document.getElementById("stringID").length; if you refer to the value of the text, I think you will get it if numeric.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38864455
@sar1973: is there a difference in the functions between java script vs. jQuery? I think in jQuery, value.length returns the numeric length of the string that is represented by value, based on the following two links and the information in them:

http://api.jquery.com/val/

http://forum.jquery.com/topic/domelem-value-length-returns-different-value-than-domelem-val-length (i hope i typed that correctly, couldn't copy and paste it)

so i'm hoping that value.length is correct. i think it is thus far. but you're saying that I don't need it.

so my jQuery would become:

                        $(".studentIdInput").keyup(function () {
                            var value = jQuery(this).val();
                            var rSt = '';
                            var lSt = '';                            
                            rSt = value.substring(8,  Min(8, value.length));
                            lSt = rSt.slice(0, -2);
                            $(".studentIdInput").val(lSt);
                        });

or do i even need the lSt = rSt.slice(0, -2); at this point?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38864499
If you use the name "value" for a variable, you may incur in conflicts with the function library. In jQuery it should be $("stringName/ID").length;
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38864628
@Sar1973: ok, so this:

$(".studentIdInput").keyup(function () {
                var value = jQuery(this).val();
                var rSt = '';
                var lSt = '';
                {
                    rSt = value.substring(8, Min(8, $(".studentIdInput").length));
                    lSt = rSt.slice(0, -2);
                    $(".studentIdInput").val(lSt);
                }
            });
0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 500 total points
ID: 38864680
Try:
$(".studentIdInput").keyup(function () {
                var myVal = jQuery(this).val();
                var myLen = jQuery(this).val().length;  //OR jQuery(this).length; OR jQuery(this).size();
                var rSt = '';
                var lSt = '';
                {
                    rSt = value.substring(8, Min(8, myLen);
                    lSt = rSt.slice(0, -2);
                    $(".studentIdInput").val(lSt);
                }
            });
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38864746
@sar1973: i did try each of the different 3 suggested ways above to get the length. the unfortunate outcome was that in all 3 cases, nothing happened when i swiped the card or when i typed in the input. nothing was trimmed or stripped out. nothing happened at all.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38864777
If you use .studentIdInput, you are calling the class, if you use #studentIdInput you call the object (see http://www.w3schools.com/jquery/jquery_selectors.asp).
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38864805
@sar1973 this is my text box

<asp:TextBox ID="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server" AutoPostBack="false" width="175px" style="margin: 0 96px 0 0;"/>

so are you suggesting that I try #txtStudentIdSearchBox instead?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38864923
It is the correct synthax to call the ID; otherwise, call with JS document.getElementById("txtStudentIdSearchBox ").length in myLen or directly in the char function.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38864951
@Sar1973: Ok, thank you for your time and input. At this point, no matter which way I reference the texbox, without that if conditional statement, absolutely nothing is happening right now when i swipe a card or type in the data. so i'm gonna just go with a clunky solution of shortening the textbox and chopping the text after another button is clicked. the original script above I posted worked perfectly when the length of the string was constant and the characters were being stripped from the right side of the string. Now with the length of the string varying and with the characters being pulled from the left end, nothing seems to be working correctly in that keyup function. I'll leave this open for a bit incase there are other suggestions but right now nothing with in my jQuery function seems to be handling this as desired.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38865062
I suggest you to use JS instead of jQuery, if you already have developed your code with JS text/char functions.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38865223
@Sara: I wish I would've. I've been using jQuery this entire time. Is there a link to some complete samples of what I'm trying to do in java script somewhere?
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38865283
I refer to W3 schools usually; you can find a complete library of string functions there.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 38865295
sounds good thanks.
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 39067179
thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question