在文本框中仅输入数字和减号 [英] Enter only numbers and minus sign in textbox
本文介绍了在文本框中仅输入数字和减号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码只输入文本框中的数字并且工作正常。
< script 类型 = text / javascript >
function fnAllowNumeric(){
if((event.keyCode < 48 || event.keyCode > 57)&& event.keyCode!= 8){
event.keyCode = 0;
alert(请输入仅限数字);
返回false;
}
}
< / script >
现在,我应该在开头输入减号。有人能建议我一些解决方案吗?
解决方案
参考本演示:仅在文本框中输入数字和减号
尝试 JavaScript isNaN()函数 [ ^ ]
+++++ [round 2] +++++
一个适应的例子:
< script >
function myFunction(){
var a = document.getElementById(txt)。value.trim() ;
if(isNaN(a)|| a ==){
alert(不是有效数字!);
} else {
alert(有效数字!);
}
}
< / script >
< 输入 type = text value = id = txt >
< 按钮 onclick = myFunction() > 点击我< / button > 跨度>
Hi,
I am using this code to enter only numbers in a textbox and works perfectly.
<script type="text/javascript">
function fnAllowNumeric() {
if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8) {
event.keyCode = 0;
alert("Please Enter Number Only");
return false;
}
}
</script>
Now on top of this I should allow minus sign to be entered at the start. Can anyone suggest me some solutions?
解决方案
Hi,
Ref This Demo : Enter only numbers and minus sign in textbox
Try JavaScript isNaN() Function[^]
+++++[round 2]+++++
An example to adapt:
<script> function myFunction() { var a = document.getElementById("txt").value.trim(); if (isNaN(a) || a == ""){ alert("Not a valid number!"); } else { alert("A valid number!"); } } </script> <input type="text" value="" id="txt"> <button onclick="myFunction()">Click me</button>
这篇关于在文本框中仅输入数字和减号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文