Some useful Regular Expression for Web UI Validation

In ASP.NET, sometimes we need to use RegularExpressionValidator for TextBox validation.

Non-literals in Regular Expression:    .    *    ?    +    (    )    {    }    [    ]    ^    $
These characters are literals when preceded by a "\". For example, \.    \*

. matches any single character except newlines.
* matches the preceding element 0 or more times.
? matches the preceding element 0 or 1 time.
+ matches the preceding element 1 or more times.

1.    Input string must contain one numeric character.

       ^[\S\s]*[0-9]+[\S\s]*$    or    ^[\S\s]*\d+[\S\s]*$

       This expresssion can be seperated into 5 parts   ^    [\S\s]*    [0-9]+    [\S\s]*   $

       ^ matches the starting position of the string

       [\S\s]* matches any number of characters (including empty string). 
       \S matches anything but a whitespace. 
       \s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029])

       [0-9]+ represents 1 ore more numeric number. This expression ensures at least 1 numeric character.
       \d is the shortcut of [0-9]
    
       [\S\s]* matches any number of characters after the numeric character.

       $ matches the ending position of the string.
        
2.    Input string must contain one uppercase character.

       ^[\S\s]*[A-Z]+[\S\s]*$

3.    Input string must contain one lowercase character.

       ^[\S\s]*[a-z]+[\S\s]*$

4.    Input string minimum length validation.

       [\S\s]{6,}

       This expression ensures the input string's minimum length is 6, and there is no limitation on maximum
       length.

       Another example on maximum length validation.
       [\S\s]{6,12}

5.    Number of Decimal points validation

       ^\d+(\.\d{0,5})?$

       This expression can be seperated as ^   \d+    (\.\d{0,5})?     $

6.     Sample Code
       
1<asp:RegularExpressionValidator ID="RegularExpressionValidatorBalance" runat="server" ControlToValidate="txtBalance" CssClass="FieldValidationError" ValidationExpression= "^\d+(\.\d{0,5})?$" Display="Dynamic">
2</asp:RegularExpressionValidator>
原文地址:https://www.cnblogs.com/zhaobin/p/1111497.html