简体   繁体   中英

Validation regular expression

Must be an integer between 10 to 100 inclusively.

<asp:TextBox ID="Donation" Columns="20" MaxLength="3"  runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
runat="server"ControlToValidate="Donation"   
ErrorMessage="Please enter valid donation" ValidationExpression="*">

This is question1,i dont know how to write the validation expression.

Regular Expressions is a wonderful tool. You can use the following links to learn how it can provide your solution. After making an attempt and posting it here, you'll likely find more assistance is forthcoming.

  1. https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions
  2. https://regex101.com/

A regex is great at finding patterns in text, but not so good at numerical values and ranges.

However, there also exists a RangeValidator

     <asp:RangeValidator id="Range1"
           ControlToValidate="Donation"
           MinimumValue="10"
           MaximumValue="100"
           Type="Integer"
           EnableClientScript="false"
           Text="The value must be from 10 to 100!"
           runat="server"/>

Be sure to set the "Type" to "Integer" in your case.


If you really must use a regular expression, you can use "[1-9][0-9]|100" :

fragment explanation
[1-9] a single digit from 1 to 9
[0-9] followed by another single digit from 0 to 9 (so together 10 - 99)
| OR
100 the exact text "100"

Note that a regex validator also checks that it matches the entire string (not just a substring, as would be the default).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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