ASP.net文本框普通防爆pression所需,包括2位小数 [英] ASP.net TextBox Regular Expression Needed to include 2 Decimal Places

查看:99
本文介绍了ASP.net文本框普通防爆pression所需,包括2位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好一切,

我有一个需要接受14位加2位小数的文本框。例子包括:结果
12345678901234结果
12345678901234.94结果
0.75

I have a textbox that needs to accept 14 digits plus 2 decimal places. Examples include:
12345678901234
12345678901234.94
.75

为此,我有以下的code:

To this end, I have the following code:

<asp:TextBox ID="txtQuantity" runat="server" CssClass="txtBox textboxRight"
    MaxLength="14" Width="70px"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
    ControlToValidate="txtQuantity" Display="Dynamic" ErrorMessage="Required "
    SetFocusOnError="True" ValidationGroup="vgItem"></asp:RequiredFieldValidator>
&nbsp;

<asp:RegularExpressionValidator ID="RangeValidator1" runat="server"
    ControlToValidate="txtQuantity" Display="Dynamic" 
    ErrorMessage="Invalid Quantity"
    ValidationExpression="^\d+(?:\.\d{0,2})?$" 
    SetFocusOnError="True"></asp:RegularExpressionValidator>

这个文本框的最大长度设置为14为好。任何人都可以指导我,要怎么写正则表达式来接受一共有14个数字和小数点后两位?

The MaxLength for this textbox is set to 14 as well. Can anyone guide me as to how to write the regex to accept a total of 14 digits and two decimal places?

谢谢,
希德

推荐答案

十位数字加(可选)期和两个数字。这是一个可以适合在你的领域最大: ^ \\ d {1,11}?(?:\\。\\ D \\ D)$

Eleven digits followed by (optionally) period and two more digits. This is the maximum that can fit in your field: ^\d{1,11}(?:\.\d\d)?$

如果您希望用户能够没有任何小数或最多两位小数键入14位到11位(而不是允许只是一个单一的十进制数字),你可以这样做: ^( ?:\\ D {1,14} | \\ d {1,11} \\ \\ D \\ D)$

If you want the user to be able to type 14 digits without any decimals or up to 11 digits with two decimals (and not allow just a single decimal digit), you could do this: ^(?:\d{1,14}|\d{1,11}\.\d\d)$

这篇关于ASP.net文本框普通防爆pression所需,包括2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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