简体   繁体   English

如何使用正则表达式验证器仅限制小数点后两位

[英]how to limit only two digits after decimal point using Regex Validator

I am using the following REG-EX validator to validate a text box for accepting only two digits after a decimal point . 我正在使用以下REG-EX验证器来验证文本框,使其仅接受小数点后的两位数字。

<asp:RegularExpressionValidator Display="Dynamic" ID="regexp" runat="server" ControlToValidate="regexptest" ValidationGroup="regexptest" ValidationExpression="^\d+(\.\d\d)?$">

But I am able to enter more than two values after decimal point. 但是我可以在小数点后输入两个以上的值。

^\d+(\.\d{2})?$ 

valid Examples are - 23, 23.12 有效的例子是-23,23.12
Invalid example- 23.2, 23.123 无效的示例-23.2、23.123

Edit: 编辑:

Please have a look at following links to restrict the user from entering more than two numbers after decimal- 请查看以下链接,以限制用户在小数点后输入两个以上的数字,

http://www.mredkj.com/tutorials/validate2.html http://www.mredkj.com/tutorials/validate2.html

http://www.mredkj.com/tutorials/validate2.js http://www.mredkj.com/tutorials/validate2.js

Either @Microsoft DJ's answer or @7alhashmi's answer should work fine provided that the decimal separator is the period and not some other character. @Microsoft DJ的答案@ 7alhashmi的答案应该可以正常工作, 只要小数点分隔符是句点而不是其他字符。 This holds for US English, but other languages and locales use different separators so for a truly internationalized solution, you cannot rely on that being the case. 这适用于美国英语,但是其他语言和语言环境使用不同的分隔符,因此对于真正的国际化解决方案,您不能依靠这种情况。 This makes it difficult to do proper validation using only a regular expression. 这使得仅使用正则表达式很难进行正确的验证。

Fortunately, there is another possibility: you can leverage decimal.TryParse() with a culture specifier together with Math.Round() . 幸运的是,还有另一种可能性:你可以利用decimal.TryParse()与文化符连同Math.Round() For example, if the user's culture is fr-FR then you could do something like: 例如,如果用户的文化是fr-FR则可以执行以下操作:

string value = "1234,56";
NumberStyles style = NumberStyles.AllowDecimalPoint;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR"); // or whatever
decimal number;

if (!Decimal.TryParse(value, style, culture, out number))
{
    failValidate(FailReason.CannotParse); // Could not parse as a number
}
else if (number != Math.Round(number, 2))
{
    failValidate(FailReason.TooManyDecimals); // More than two decimals provided
}
else
{
    succeedValidate(); // Number was valid and has at most two decimals
}

Of course, this technique is only really useful on the server side (where you should be doing all the validation that you rely on anyway; client-side validation is a convenience for the user, not something to depend upon for security or input validation ). 当然,此技术仅在服务器端真正有用(无论如何,您应该在此进行所有依赖的验证; 客户端验证对用户来说是一种方便,而不是依赖于安全性或输入验证的东西 ) 。

Using Math.Round() as above will catch the case of the user entering 12.001 , but 12.000 will be accepted since it is equal to 12.00 or for that matter 12 . 使用Math.Round()如上述将捕获进入用户的情况下12.001 ,但12.000将被接受,因为它是等于12.00或就此12 Depending on your usage scenario this may or may not be acceptable. 根据您的使用情况,这可能会接受也可能不会接受。

Try this: 尝试这个:

<asp:RegularExpressionValidator Display="Dynamic" ID="regexp" runat="server" 
     ControlToValidate="regexptest" ValidationGroup="regexptest" 
     ValidationExpression="^\d+(\.\d{1,2})?$">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM