Solved

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

Posted on 2013-02-06
23
414 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
  • 12
  • 9
  • 2
23 Comments
 
LVL 1

Expert Comment

by:thisstupidservicewontletmeusemyname
Comment Utility
use:

rSt = value.substring(1,8);

to get characters 2-9.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
@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

Expert Comment

by:thisstupidservicewontletmeusemyname
Comment Utility
It will work with any length string. Use substring(1,8) for characters 2-9, substring(4,10) for characters 5-11 etc.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
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
Comment Utility
You can use Min(8, string.length) as 2nd argumento of the function instead.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
@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
Comment Utility
I refer to W3 schools usually; you can find a complete library of string functions there.
0
 
LVL 1

Author Comment

by:mikesExpertExchange
Comment Utility
sounds good thanks.
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
Comment Utility
thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now