本文介绍了使用javascript将文本框值限制为数字,也只限制一个小数点。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在我的项目中使用表单,并且在该表单中有一个文本框,该文本框应该只包含数字,并且只有一个小数点数字或者只是正常的整数,例如:10,10.55,11.66左右......前者不应该超过一个小数点:10.5.4不应该采取。 我用javascript做了如下: function isNumberKey(evt) { var charCode =(evt.which)? evt.which: event .keyCode; if (charCode!= 46 && charCode > 31 &&(charCode < 48 || charCode > 57 )) return false ; return true ; } 这只允许数字,也指向。所以它需要任意数量的点来限制我使用像这样的js: function checkDec(el){ var ex = /^ [0-9] + \。?[0-9] * $ /; if (ex.test(el。 value )== false ){ alert(' 错误的数字') ; } 我使用的文本框是这样的: < input type =textclass =TextBoxrunat =serverid =txtDateFromonkeypress =return isNumberKey(event);的onblur = checkDec(本); /> 但这不是将小数点限制为单个小数点。 PLease帮助我如何限制用户并允许他们输入一个小数点....我应该使用任何onclick事件?我喜欢的是一个onkeypress事件检查...这可能请帮助... 提前感谢你!! 解决方案 /; if (ex.test(el。 value )== false ){ alert(' 错误的数字') ; } 我使用的文本框是这样的: < input type =textclass =TextBoxrunat =serverid =txtDateFromonkeypress =return isNumberKey(event);的onblur = checkDec(本); /> 但这不是将小数点限制为单个小数点。 PLease帮助我如何限制用户并允许他们输入一个小数点....我应该使用任何onclick事件?我喜欢的是一个onkeypress事件检查...这可能请帮助... 提前感谢你!! 使用FilteredTextBox ajax控件 Hi,I am using a form in my project and in that form there is a text box which should take only numbers and also only one decimal point numbers or just normal integers for ex: 10, 10.55, 11.66 or so on... it should not take more than a single decimal point for ex: 10.5.4 should not take.I had done it using javascript as follows:function isNumberKey(evt){ var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true;}this allows only numbers and also point that is "." so it takes any number of points to restrict i used like this js:function checkDec(el) { var ex = /^[0-9]+\.?[0-9]*$/; if (ex.test(el.value) == false) { alert('Incorrect Number'); }the text box i used was like this :<input type="text" class="TextBox" runat="server" id="txtDateFrom" onkeypress="return isNumberKey(event);" onblur="checkDec(this);" />But this is not restricting the decimal point to single decimal point. PLease help me with how i can i restrict user and allow them to enter a single decimal point .... and should i use any onclick event ? what i prefer ia an onkeypress event check ... Is that Possible please help...Thanking You in advance!! 解决方案 /; if (ex.test(el.value) == false) { alert('Incorrect Number'); }the text box i used was like this :<input type="text" class="TextBox" runat="server" id="txtDateFrom" onkeypress="return isNumberKey(event);" onblur="checkDec(this);" />But this is not restricting the decimal point to single decimal point. PLease help me with how i can i restrict user and allow them to enter a single decimal point .... and should i use any onclick event ? what i prefer ia an onkeypress event check ... Is that Possible please help...Thanking You in advance!!use FilteredTextBox ajax control 这篇关于使用javascript将文本框值限制为数字,也只限制一个小数点。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 14:05