Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

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.
Avatar of thisstupidservicewontletmeusemyname
thisstupidservicewontletmeusemyname

use:

rSt = value.substring(1,8);

to get characters 2-9.
Avatar of Michael Sterling

ASKER

@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)
It will work with any length string. Use substring(1,8) for characters 2-9, substring(4,10) for characters 5-11 etc.
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.
You can use Min(8, string.length) as 2nd argumento of the function instead.
@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.
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"?).
@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?
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.
@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?
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;
@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);
                }
            });
ASKER CERTIFIED SOLUTION
Avatar of Sar1973
Sar1973
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.
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).
@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?
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.
@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.
I suggest you to use JS instead of jQuery, if you already have developed your code with JS text/char functions.
@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?
I refer to W3 schools usually; you can find a complete library of string functions there.
sounds good thanks.
thanks