admin管理员组文章数量:1405187
There'a way in javascript to allow the user input in a text field (input type="text") only digits but optionally having the minus before them? (I.e. only negative and positive number)
There'a way in javascript to allow the user input in a text field (input type="text") only digits but optionally having the minus before them? (I.e. only negative and positive number)
Share asked Nov 8, 2012 at 13:27 Sasha GrievusSasha Grievus 2,6866 gold badges34 silver badges61 bronze badges5 Answers
Reset to default 2<input type='text' onkeypress='return numbersOnly(this,event,false,true);'>
function numbersOnly(Sender,evt,isFloat,isNegative) {
if(Sender.readOnly) return false;
var key = evt.which || !window.event ? evt.which : event.keyCode;
var value = Sender.value;
if((key == 46 || key == 44) && isFloat){
var selected = document.selection ? document.selection.createRange().text : "";
if(selected.length == 0 && value.indexOf(".") == -1 && value.length > 0) Sender.value += ".";
return false;
}
if(key == 45) { // minus sign '-'
if(!isNegative) return false;
if(value.indexOf('-')== -1) Sender.value = '-'+value; else Sender.value = value.substring(1);
if(Sender.onchange != null) {
if(Sender.fireEvent){
Sender.fireEvent('onchange');
} else {
var e = document.createEvent('HTMLEvents');
e.initEvent('change', false, false);
Sender.dispatchEvent(e);
}
}
var begin = Sender.value.indexOf('-') > -1 ? 1 : 0;
if(Sender.setSelectionRange){
Sender.setSelectionRange(begin,Sender.value.length);
} else {
var range = Sender.createTextRange();
range.moveStart('character',begin);
range.select();
}
return false;
}
if(key > 31 && (key < 48 || key > 57)) return false;
}
Related question: HTML Text Input allow only Numeric input
You can bind a listener (in javascript, that is) to the onKeyUp event (or the one that best fits your needs) and check what is the user trying to insert. Then you can decide if you'll let him insert that character or not.
NOTE: You'll want to control also the copy/paste event as the user could paste some text instead of writing it!
Use this. It works.
https://raw.github./SamWM/jQuery-Plugins/master/numeric/jquery.numeric.js
$(document).ready(function(){
$(".numeric").numeric({negative :true});
});
Try the following regexp:
/^-?\d+$/
This is the best solution I came up with for positive and negative integers only
<input type="text" onkeyup="this.value = (this.value[0] === '-') ? ('-' + this.value.replace(/[^0-9]/g, '')) : (this.value.replace(/[^0-9]/g, ''));" pattern="^-?\d+" />
You can check it out here http://jsfiddle/4g08ofz6/
The pattern
pattern="^-?\d+"
is just an extra layer of validation for when the value is pasted in or dragged in but remember you should still always validate on the server site
本文标签: regexJavascript limit text field to positive and negative numbersStack Overflow
版权声明:本文标题:regex - Javascript limit text field to positive and negative numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744882246a2630275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论