在Asp.Net的文本框中只允许一个小数点.如何编写javascript函数? [英] allow only one decimal point in a Text Box in Asp.Net. how to write javascript function?

查看:94
本文介绍了在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 to double.TryParse(). If it returns false, the value could not be converted to a double (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]*


这篇关于在Asp.Net的文本框中只允许一个小数点.如何编写javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆