javascript 如何在文本框中只允许十进制数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20162377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to allow only decimal numbers in a TextBox
提问by vivek jain
I am working on asp.net web application.
我正在研究 asp.net Web 应用程序。
I have a textbox in that textbox I need to allow decimal number before pressing the submit button. so how to do this at client side before submitting the submit button.
我在该文本框中有一个文本框,我需要在按下提交按钮之前允许十进制数。那么如何在提交提交按钮之前在客户端执行此操作。
if it could be done by javascript, ajax aur from the code behind please let me know.
如果它可以通过javascript完成,来自背后代码的ajax aur请让我知道。
回答by user3024816
You can do it two way, either through built in validation of asp.net or using javascript. Through javascript you can do it as follows (This will prevent user from entering character key, hence only numbers will be allowed):
您可以通过两种方式来实现,一种是通过 asp.net 的内置验证,另一种是使用 javascript。通过javascript,您可以执行以下操作(这将阻止用户输入字符键,因此只允许输入数字):
<asp:Text onkeypress="return isNumber(event);" runat="server"/>
<script>
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
</script>
回答by Joe Brunscheon
Rather than writing your own custom validation code in javascript or server-side validation, there are libraries out there that allow you to do the validation client side with jquery.
不是在 javascript 或服务器端验证中编写自己的自定义验证代码,而是有一些库允许您使用 jquery 进行客户端验证。
Add these script references (you would need jquery as well):
添加这些脚本引用(您还需要 jquery):
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
Then you can do the following:
然后您可以执行以下操作:
<script type="text/javascript>
$(document).ready(function()
{
$("#idOfMyForm").validate(
{
rules: {
IdOfMyTextBox: {
required: true,
number: true
}
}
});
$("#submitButton").click(function(e){
if(!$("#idOfMyForm").valid(){
e.preventDefault(); //stop the form from submitting
alert("My form is not valid.");
}
//continue submitting the form, do whatever you need to do.
});
});
</script>
回答by varocarbas
If you want a code-behind alternative, you can rely on TryParse
. Sample code for Button1
and TextBox1
:
如果你想要一个代码隐藏的替代方案,你可以依靠TryParse
. 示例代码Button1
和TextBox1
:
string currentDot1 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string currentDot2 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
string currentDot3 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.PercentDecimalSeparator;
decimal curVal = 0m;
if (TextBox1.Text.Trim().Length > 0 && (TextBox1.Text.Contains(currentDot1) || TextBox1.Text.Contains(currentDot2) || TextBox1.Text.Contains(currentDot3)) && decimal.TryParse(TextBox1.Text, out curVal))
{
//curVal is valid decimal
}
else
{
//NO DECIMAL
}