zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    HDOj-1412
    HDOJ-2153
    HDOJ-1002
    紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)
    紫书 例题11-4 UVa247 (Floyd判断联通)
    最短路模板
    紫书 例题 11-3 UVa 1151 (有边集的最小生成树+二进制枚举子集)
    紫书 例题 11-2 UVa 1395(最大边减最小边最小的生成树)
    紫书 例题 11-1 UVa 12219 (表达式树)
    紫书 习题 8-25 UVa 11175 (结论证明)(配图)
  • 原文地址:https://www.cnblogs.com/zhaobin/p/1111497.html
Copyright © 2011-2022 走看看