Good Evening All,
I have a textbox that needs to accept 14 digits plus 2 decimal places. Examples include:
12345678901234
12345678901234.94
.75
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>
<asp:RegularExpressionValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtQuantity" Display="Dynamic"
ErrorMessage="Invalid Quantity"
ValidationExpression="^d+(?:.d{0,2})?$"
SetFocusOnError="True"></asp:RegularExpressionValidator>
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?
Thanks,
Sid
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Eleven digits followed by (optionally) period and two more digits. This is the maximum that can fit in your field: ^d{1,11}(?:.dd)?$
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}.dd)$
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0