admin管理员组

文章数量:1429578

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

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

Share Improve this question edited Dec 15, 2014 at 19:22 nicolefdesigns asked Dec 15, 2014 at 19:01 nicolefdesignsnicolefdesigns 852 silver badges10 bronze badges 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. – G. Cito 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. – nicolefdesigns Commented Dec 15, 2014 at 19:22
Add a ment  | 

2 Answers 2

Reset to default 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/jhjr288o/4/

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

本文标签: javascriptUsing substring within input fieldStack Overflow