[英]Asp.net How to limit number of digits before and after the decimal point in textbox keypress event
I have a Gridview with a textbox. 我有一个带文本框的Gridview。 How do I limit the number of digits before and after the decimal point in a textbox keypress event ?
如何限制文本框按键事件中小数点前后的位数? I want a maximum of 6 digits before the decimal point and 2 digits after the decimal point.
我希望小数点前最多6位,小数点后2位。 How do I do this with javascript/JQuery?
我如何使用javascript / JQuery执行此操作?
I personally do not like wiping out the data as soon as a user types something. 我个人不喜欢在用户输入内容时立即删除数据。
Instead, I like to display an error message after they finish entering data. 相反,我喜欢在完成输入数据后显示错误消息。
For example, 例如,
<asp:TextBox runat="server" ID="TextBox1" MaxLength="9" />
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1"
ValidationExpression="^((\d{5})*|([1-9]\d{0,5}))(\.\d{0,2})?$"
ControlToValidate="TextBox1" Text="Input must be 123456.78 format."
Display="Dynamic" />
Most simple javascript validation vanilla style check 最简单的javascript验证香草风格检查
http://jsfiddle.net/InferOn/8aCJc/ http://jsfiddle.net/InferOn/8aCJc/
HTML HTML
<input type="text" id="myInput" onkeyup="check()" />
Javascript 使用Javascript
function check() {
var check = true;
var txt = document.getElementById('myInput');
tmp = txt.value;
if (tmp && tmp.length > 0) {
arg = tmp.split('.');
if (arg && arg.length > 0) {
check = arg[0].length <= 6;
if (arg.length > 1 && check) {
check = arg[1].length <= 2;
}
}
}
if (!check)
alert('check failed');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.