admin管理员组文章数量:1024966
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
2 Answers
Reset to default 4You 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
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
2 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - Using substring within input field - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745507995a2153694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论