ASP. NET Validation Control Regular Expression Special Symbol Description

  • 2021-07-16 02:09:12
  • OfStack

RegularExpressionValidator control expression description:

The square brackets "[]" are used to define acceptable characters. [abc123] indicates that the control can only accept 6 characters a, b, c, 1, 2, and 3;

The inverse set symbol "^" is used to define unacceptable characters. [^ a-h] indicates that the control is acceptable except for 8 characters from a to h;

The curly brace '{}' defines the number of characters that must be entered. {6} indicates that only 6 characters can be entered; {6,} indicates that more than 6 must be entered, with no upper limit; {2, 6} indicates that 2 to 6 characters must be entered; However, curly brackets must be placed after square brackets, such as [a-z] {4} to indicate that any character between 4 bits a and z must be entered.

The dot "." is used to represent any character. For example. {3, 6} means accepting 3 to 6 arbitrary characters.

The vertical bar "" is used to indicate the logical symbol of "or". For example, [1-9] {3, 6} [A-Za-z] {3} indicates that 3 to 6 numbers or 3 letters can be accepted. (You can distinguish between case and case)

Parentheses "()" are used for chunking, which is similar to parentheses in numeric operations.

Slash "\" If you want acceptable characters to contain the above special characters. For example,\ ([0-9] {3}\) indicates a phone area code in the format "(xxx)".

Code demo:


<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %>
<head>
<title>validation Validation control </title>
</head>
<body>
<form runat="server">
  <p> Enter a name :
  <asp:TextBox ID="nam" Text=" Name " TextMode="SingleLine" runat="server" /> <asp:RequiredFieldValidator ControlToValidate="nam" Display="Dynamic" ErrorMessage=" You must enter a name " ID="rnam" runat="server" /></p>
  <p> Enter date :
    <asp:TextBox ID="dat" Text=" Date " TextMode="SingleLine" runat="server" /> <asp:CompareValidator ControlToValidate="dat" Display="Dynamic" ErrorMessage=" The correct format for a date is yyyy-mm-dd" ID="rdat" Operator="DataTypeCheck" runat="server" Type="Date" />     </p>
  <p> Enter a numeric value (integer) :
  <asp:TextBox ID="type" Text=" Numerical value " TextMode="SingleLine" runat="server" />  <asp:CompareValidator ControlToValidate="type" Display="Dynamic" ErrorMessage=" What you entered is not 1 Integer " ID="rtype" Operator="DataTypeCheck" runat="server" Type="Integer" />    </p>
  <p> Enter a number ( 1-10 ) :
  <asp:TextBox ID="num" Text=" Numerical value " TextMode="SingleLine" runat="server" />    <asp:RangeValidator ControlToValidate="num" Display="Dynamic" ErrorMessage=" It can only be 1-10 Number between " ID="rnum" Type="Integer" MaximumValue="10" MinimumValue="1" runat="server" />  </p>
  <p> Please enter 1 Less than 50 Number of :
    <asp:TextBox ID="num5" Text=" Less than 50 Adj. " TextMode="SingleLine" runat="server" />   
    <asp:CompareValidator ControlToValidate="num5" Display="Dynamic" ErrorMessage=" The number you entered is not less than 50" ID="rnum5" Operator="LessThan" runat="server" Type="Integer" ValueToCompare="50" /></p>
  <p> Random input 1 A email :
    <asp:TextBox ID="eml" Text="email" TextMode="SingleLine" runat="server" />   
    <asp:RegularExpressionValidator ControlToValidate="eml" Display="Dynamic" ErrorMessage=" It should be 3-6 Arbitrary character @2-9 Bit arbitrary character .2-3 Bit arbitrary character " ID="reml" runat="server" ValidationExpression=".{3,6}@.{2,9}\..{2,3}" />
  <asp:ValidationSummary DisplayMode="List" HeaderText=" Error message set " ID="tt" runat="server" /></p>
  <p>
  <asp:Button ID="But" Text=" Submit " runat="server" />      </p>
  </p>
</form>
</body>
</html>


Related articles: