Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-06
23
Medium Priority
?
425 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:Michael Sterling
  • 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:Michael Sterling
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Michael Sterling
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:Michael Sterling
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:Michael Sterling
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:Michael Sterling
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:Michael Sterling
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 2000 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:Michael Sterling
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:Michael Sterling
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:Michael Sterling
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:Michael Sterling
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:Michael Sterling
ID: 38865295
sounds good thanks.
0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 39067179
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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

963 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