2

I'm using an ID scanner that acts as a keyboard input and I want an input field to listen for the value and substring (or slice) out unnecessary, extra values.

Currently, the ID scanner formats numbers like this: ;708089113=0184?

I want to grab only the 708089113. I want to remove the semi-colon and everything after the 3.

I'm just not sure how to do this automatically. The stripped value should appear in the field before submitting.

Javascript:

var suid = document.getElementById("SUID").value;
var stripSUID = suid.substring(1,10); 

document.getElementById("SUID").value = stripSUID;

HTML:

<input name="SUID" id="SUID" type="text" value="">

JSFiddle Link

2
  • See the answer by @Paul S. below. Can you clarify in your question what you are expecting to happen. If you want the "stripped value .. [to] ... appear in the field before submitting", then you want to listen for the .value to change in the <input> field. A more accurate question makes the resulting Q/A more useful for others. Commented Dec 15, 2014 at 19:14
  • @G.Cito Wasn't exactly sure what attribute I needed, so thank you for explaining the listen function. I have updated my question. Commented Dec 15, 2014 at 19:22

2 Answers 2

4

You can use jQuery for this.

HTML:

<input name="suid" id="suid" type="text" value="">

JavaScript:

$(function() {
    $('#suid').change(function() {
        var suid = $(this).val();
        var stripSUID = suid.split('=');
        var stringLength = stripSUID[0].length;
        var returnValue = stripSUID[0].substr(1, stringLength);

        $(this).val(returnValue);
    });
});

jsFiddle update: http://jsfiddle.net/jhjr288o/4/

Sign up to request clarification or add additional context in comments.

5 Comments

But he needs it, on the event change.
I have modified my answer.
@KrzysztofTrzos Working so far, but it's still grabbing the semi-colon (;) before the numbers. I need that stripped too.
@nicolefdesigns Didn't noticed that. Answer updated: jsfiddle.net/jhjr288o/4
@KrzysztofTrzos Thanks for updating your answer. I appreciate your knowledge. I always prefer to use jQuery, but I'm using Sharepoint (not by choice) to inject this code, so it doesn't seem to play nicely with it. I'll leave this as the best answer though.
2

So you're asking how to listen for a change to the <input>?

var elm = document.getElementById('SUID');

elm.addEventListener('change', function (e) {
    var s = this.value, i = s.indexOf('=');
    if (i !== -1) {
        s = s.slice(1, i);
        this.value = s;
    }
});

DEMO

The change event fires when the element loses focus
The input event fires every time oldvalue !== newvalue (i.e. for every char typed)


Also note, this code must be run after the Element exists, i.e. wait for the Window's load event

5 Comments

It's fun typing = in and watching the chars disappear from the front, too! OP, you may also want to check s.charAt(0) === ';'
Are there more tags that can be added to the question here?
@PaulS. This is a great answer too. However, I'm trying to modify your code so that the equal sign and everything after it get stripped away. Your code currently keeps all the numbers and the question mark at the end. Any suggestions on how to achieve this with your code?
When you said acts as a keyboard input I assumed that it typed the whole thing in one go, if it's typing letter by letter, perhaps change is the event you want to listen for. @nicolefdesigns hows this new version?
@PaulS. Perfect! Thank you very much! Unfortunately, the ID scanners are older technology and I can't use newer ones, so I always have to adjust the code for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.