ASP.NET - Validators - Regular Expression

The RegularExpressionValidator control is used to validate that user input matches a specific pattern, specified using a regular expression. It's useful for cases where you need to validate user input based on a specific format, such as validating email addresses, phone numbers, or postal codes.

Here are some key properties, methods, and events of the RegularExpressionValidator control:

  • ControlToValidate: Specifies the ID of the control that needs to be validated.
  • ValidationExpression: Specifies the regular expression pattern that the input must match.
  • ErrorMessage: Specifies the error message that should be displayed if the input doesn't match the pattern.
  • Text: Specifies the text to validate. If this property is set, the ControlToValidate property is ignored.
  • Type: Specifies the data type that the input should be converted to before performing the validation.
  • ValidationGroup: Specifies the name of the validation group that this validator belongs to. Validators are grouped together to allow you to perform validation on different parts of a form separately.
  • Enabled: Determines whether the validator is enabled or disabled.
  • IsValid: Returns a Boolean value indicating whether the validation was successful or not.
  • Validate(): Performs the validation and returns a Boolean value indicating whether the validation was successful or not.
  • ServerValidate: An event that is raised when the validation is performed on the server. You can use this event to perform custom validation logic.

Example 1: Validating an Email Address

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="revEmail" runat="server" 
     ControlToValidate="txtEmail" 
     ErrorMessage="Please enter a valid email address." 
     ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>

In this example, we're using the RegularExpressionValidator control to validate that the user enters a valid email address. The ValidationExpression property contains a regular expression that matches most standard email address formats. If the user enters an invalid email address, the error message specified in the ErrorMessage property will be displayed.

Example 2: Validating a Password

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RegularExpressionValidator ID="revPassword" runat="server"
     ControlToValidate="txtPassword"
     ErrorMessage="Please enter a password with at least one uppercase letter, one lowercase letter, and one number."
     ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$">
</asp:RegularExpressionValidator>

In this example, we're using the RegularExpressionValidator control to validate that the user enters a password that meets specific requirements. The ValidationExpression property contains a regular expression that matches passwords with at least one uppercase letter, one lowercase letter, and one number, and is at least 8 characters long. If the user enters a password that doesn't meet these requirements, the error message specified in the ErrorMessage property will be displayed.