当我输入Charchter而不是数字我需要显示消息时请“输入数字“使用Javascript [英] When I Enter A Charchter Instead Of Number I Need To Display Message Please " Enter Number" Using Javascript
问题描述
HI,
i希望在我的应用程序中验证如果输入字符而不是数字,则显示消息输入数字....
HI ,
i want to validate in my application tht it shd display message enter number if i enter a character instead of number ....
推荐答案
<!DOCTYPE html>
<html>
<head>
<script>
function changeFunction() {
var reg = /[1-9]/g
input_val = document.getElementById("input_box_id").value
if(!reg.test(input_val) ){ //true if non numeric value
alert("Please enter valid number");
}
}
</script>
</head>
<body>
Number : <input type="text" id="input_box_id" onchange="changeFunction()">
</body>
</html>
http://www.aspsnippets.com/Red/Articles/Numeric-Validation -Accept-only-numeric-values-numbers-in-TextBox-using-JavaScript.aspx [ ^ ]
如果你'重新使用ASP.NET 4.0或更高版本,您只需要将type =number
添加到TextBox
:
If you're using ASP.NET 4.0 or later, you just need to addtype="number"
to yourTextBox
:
<asp:TextBox runat="server" type="number" />
这将呈现数字输入类型:
http://www.html5tutorial.info/html5-number.php [ ^ ]
所有现代浏览器都支持它:
http://caniuse.com/#feat=input-number [ ^ ]
如果你需要支持旧浏览器,有一个polyfill可用:
https://github.com/jonstipe / number-polyfill [ ^ ]
这个还有一个额外的好处是,在移动设备上,用户在点击输入时会默认看到数字键盘。
This will render a numeric input type:
http://www.html5tutorial.info/html5-number.php[^]
It's supported in all modern browsers:
http://caniuse.com/#feat=input-number[^]
If you need to support older browsers, there is a polyfill available:
https://github.com/jonstipe/number-polyfill[^]
This has the added benefit that, on mobile devices, the user will see the numeric keyboard by default when they tap on the input.
这篇关于当我输入Charchter而不是数字我需要显示消息时请“输入数字“使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!