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

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.
LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?
 
Sar1973Commented:
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
 
thisstupidservicewontletmeusemynameCommented:
use:

rSt = value.substring(1,8);

to get characters 2-9.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
thisstupidservicewontletmeusemynameCommented:
It will work with any length string. Use substring(1,8) for characters 2-9, substring(4,10) for characters 5-11 etc.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
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
 
Sar1973Commented:
You can use Min(8, string.length) as 2nd argumento of the function instead.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
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
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
I suggest you to use JS instead of jQuery, if you already have developed your code with JS text/char functions.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@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
 
Sar1973Commented:
I refer to W3 schools usually; you can find a complete library of string functions there.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
sounds good thanks.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.