在Asp.Net的文本框中只允许一个小数点.如何编写javascript函数?
问题描述:
在Asp.Net的文本框中仅允许一个浮点数.如何编写Java脚本功能?
例子
用户输入12..3或15.2.或..54这次Validate..So如何验证JavaScript中使用的..任何示例?帮助我...
-------
仅在我的视图"用户中输入数字
allow only one float point in a Text Box in Asp.Net. how to write java script function?
Example
A User Enter 12..3 or 15.2. or ..54 this time Validate..So how to Validate used in JavaScript..Any Examples?Help Me...
-------
in My View User Only Enter one decimal Point between the number
答
you can achieve your requirement by using combination of Javascript function & ajax FilteredTextBoxExtender.
Add following java script function in your .aspx file
<script type="text/javascript">
function IsOneDecimalPoint(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode; // restrict user to type only one . point in number
var parts = evt.srcElement.value.split('.');
if(parts.length > 1 && charCode==46)
return false;
return true;
}
</script>
Write following tag for textbox & ajax FilteredTextBoxExtender.
<asp:TextBox ID="TextBoxValue" runat="server" onkeypress="return IsOneDecimalPoint(event);">
<ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender" runat="server" FilterType="Custom"
ValidChars="01234567890." TargetControlID="TextBoxValue"></ajax:FilteredTextBoxExtender>
-Add other tags as per your requirments..
- Now user can only type o to 9 & only one decimal point(.)
您的.Net验证应包括对double.TryParse()
的调用.如果返回false,则无法将值转换为double
(两个小数点将导致其返回false).
Your .Net validation should include a call todouble.TryParse()
. If it returns false, the value could not be converted to adouble
(and a two decimal points would cause it to return false).
这里是您可以使用的javascript函数
Here''s a javascript function you can use
<script>
function checkDec(el){
var ex = /^[0-9]+\.?[0-9]*