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>
  • 相关阅读:
    day02:Activity
    day01:Android快速入门
    解读经典《C#高级编程》第七版 Page79-93.对象和类型.Chapter3
    解读经典《C#高级编程》第七版 Page68-79.对象和类型.Chapter3
    基于.Net进行前端开发的技术栈发展路线(一)
    解读经典《C#高级编程》第七版 Page50-68.核心C#.Chapter2
    解读经典《C#高级编程》第七版 Page45-50.核心C#.Chapter2
    解读经典《C#高级编程》第七版 Page38-45.核心C#.Chapter2
    解读经典《C#高级编程》第七版 Page32-38.核心C#.Chapter2
    解读经典《C#高级编程》第七版 Page20-32.核心C#.Chapter2
  • 原文地址:https://www.cnblogs.com/zhaobin/p/1111497.html
Copyright © 2011-2022 走看看