简体   繁体   English

Asp.net如何限制textbox按键事件中小数点前后的位数

[英]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.

相关问题 ASP.NET中TextBox的KeyPress事件 - KeyPress Event of TextBox in ASP.NET 如何限制 asp.net 文本框中的最大和最小文本数? - How to limit maximum and minimum number of text in asp.net textbox? 如何在Windows Phone 8中强制TextBox允许点后有两位数字的十进制数字 - How to force a TextBox in windows phone 8 to allow a decimal number with two digits after point 如何限制文本框在允许小数点前最多6位和小数点后2位之后输入十进制值 - How to restrict textbox to enter Decimal values where before decimal upto 6 digits are allowed and after decimal 2 digits 如何使用ajax处理asp.net中的TextBox.KeyPress事件 - how to handle TextBox.KeyPress event in asp.net using ajax 获取小数点前的位数 - Get number of digits before decimal point 如何通过本机 JavaScript 在 asp.net 文本框中的 3 位数字后放置点? - How can I put dot after 3 digits in asp.net textbox by native JavaScript? 如何使用C#或ASP.net从客户端调用私有void按键事件处理程序,以仅允许数字进入文本框 - How to call a private void keypress event handler from client side using C# or ASP.net to allow only numbers into textbox asp文本框没有按键事件 - No keypress event for asp textbox double.ToString(???) - 格式化以在小数点前删除数字并在小数点后显示 2 位数字? - double.ToString(???) - format to drop digits BEFORE decimal point and show 2 digits AFTER decimal point?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM