Home » Development » How to validate user input by using RegularExpressionValidator in ASP.NET

How to validate user input by using RegularExpressionValidator in ASP.NET

Here is an easy way to check user inputs in ASP.NET: RegularExpressionValidator control. You can compare the input with the regular expression you provide and show users corresponding validation errors.

Validation Rules

In this example, my validation rules are below.

Field Required? Allowed Characters Min Max
Email Yes a-z, A-Z, 0-9, at-sign (@), period (.), hyphen (-), underscore (_) 8 50
First Name Yes a-z, A-Z, period (.), apostrophe (‘), hyphen (-), space ( ), ISO/IEC 8859-1 chars 2 50
Last Name Yes a-z, A-Z, period (.), apostrophe (‘), hyphen (-), space ( ), ISO/IEC 8859-1 chars 2 50
Job Title Yes a-z, A-Z, period (.), apostrophe (‘), ampersand (&), parenthesis (()), slashes (/\), hyphen (-), space ( ), ISO/IEC 8859-1 chars 2 50
Department Yes a-z, A-Z, period (.), apostrophe (‘), ampersand (&), parenthesis (()), slashes (/\), hyphen (-), space ( ), ISO/IEC 8859-1 chars 2 50
Organization Yes a-z, A-Z, period (.), apostrophe (‘), ampersand (&), parenthesis (()), slashes (/\), hyphen (-), space ( ), ISO/IEC 8859-1 chars 2 50
Phone Yes 0-9, period (.), space ( ), parenthesis (()), plus (+), hyphen (-), forward slash (/) 2 20
Fax No 0-9, period (.), space ( ), parenthesis (()), plus (+), hyphen (-), forward slash (/) 2 20
Website No a-z, A-Z, 0-9, http://, https://, period (.), hyphen (-), underscore (_) 4 50

Validation Controls

ASP.NET code for input controls and validation controls are below. Please note that I used RequiredFieldValidator as well as RegularExpressionValidator.

 

<asp:ValidationSummary ID="smryUpdate" EnableClientScript="true" ShowSummary="true" ValidationGroup="smryUpdate" HeaderText="Please correct the following errors:" DisplayMode="BulletList" runat="server" >

<telerik:RadTextBox ID="txtEmail" runat="server" Font-Size="Medium" Width="320px"></telerik:RadTextBox>
<asp:RequiredFieldValidator CssClass="ValidationMessage" ID="requiredEmail" SetFocusOnError="true" Display="Dynamic" runat="server" Text="Email is required" ErrorMessage="Email is required" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator CssClass="ValidationMessage" ID="regexEmail" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z0-9]+[\w-]+\.)+[a-zA-Z]{1}[a-zA-Z0-9-]{1,23})$" ControlToValidate="txtEmail" ErrorMessage="Invalid email format"></asp:RegularExpressionValidator>
<asp:CustomValidator CssClass="ValidationMessage" ID="customValidatorEmail" ControlToValidate="txtEmail" ValidationGroup="smryUpdate" ClientValidationFunction="ClientValidate" Display="Dynamic" ErrorMessage="Please do not use an address from a public email provider (Gmail, Yahoo, Hotmail etc.)" runat="server" />

<telerik:RadTextBox ID="txtFirstName" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="requiredFirstName" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" Text="First name is required" ErrorMessage="First name is required" ControlToValidate="txtFirstName" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexFirstName" CssClass="ValidationMessage" SetFocusOnError="true" runat="server" Display="Dynamic" ValidationExpression="^[a-zA-Z'\s.\u00c0-\u017e-]{2,50}$" ControlToValidate="txtFirstName" ErrorMessage="Invalid name format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtLastName" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="requiredLastName" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" Text="Last name is required" ErrorMessage="Last name is required" ControlToValidate="txtLastName" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexLastName" CssClass="ValidationMessage" SetFocusOnError="true" runat="server" Display="Dynamic" ValidationExpression="^[a-zA-Z'\s.\u00c0-\u017e-]{2,50}$" ControlToValidate="txtLastName" ErrorMessage="Invalid name format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtJobTitle" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="requiredJobTitle" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" Text="Job title is required" ErrorMessage="Job title is required" ControlToValidate="txtJobTitle" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexJobTitle" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^[a-zA-Z'\s.&/\\()\u00c0-\u017e-]{2,50}$" ControlToValidate="txtJobTitle" ErrorMessage="Invalid job title format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtDepartment" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="requiredDept" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" Text="Department is required" ErrorMessage="Department is required" ControlToValidate="txtDepartment" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexDept" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^[a-zA-Z'\s.&/\\()\u00c0-\u017e-]{2,50}$" ControlToValidate="txtDepartment" ErrorMessage="Invalid department format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtOrganization" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="requiredOrganization" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" Text="Organization is required" ErrorMessage="Organization is required" ControlToValidate="txtOrganization" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexOrganization" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^[a-zA-Z'\s.&/\\()\u00c0-\u017e-]{2,50}$" ControlToValidate="txtOrganization" ErrorMessage="Invalid organization format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtPhone" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="requiredPhone" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" Text="Phone is required" ErrorMessage="Phone is required" ControlToValidate="txtPhone" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexPhone" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^[0-9.()\-+/\s]{2,20}$" ControlToValidate="txtPhone" ErrorMessage="Invalid phone format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtFax" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" CssClass="ValidationMessage" runat="server" Display="Dynamic" ControlToValidate="RadTextBox5" ErrorMessage="The textbox cannot be empty" EnableClientScript="true"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexFax" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^[0-9.()\+/\s-]{2,20}$" ControlToValidate="txtFax" ErrorMessage="Invalid fax number format"></asp:RegularExpressionValidator>

<telerik:RadTextBox ID="txtWebsite" runat="server" Font-Size="Medium" Width="200px"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" CssClass="ValidationMessage" runat="server" Display="Dynamic" ControlToValidate="RadTextBox7" ErrorMessage="The textbox cannot be empty" EnableClientScript="true"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexWebsite" CssClass="ValidationMessage" SetFocusOnError="true" Display="Dynamic" runat="server" ValidationExpression="^(https?:\/\/[a-zA-Z0-9\-_\.]{1,32}\.[a-zA-Z0-9\-_]{2,9}[a-zA-Z0-9\-_./]{0,30}|[a-zA-Z0-9\-_.]{1,40}\.[a-zA-Z0-9\-_]{2,9}[a-zA-Z0-9\-_./]{0,30})$" ControlToValidate="txtWebsite" ErrorMessage="Invalid website format"></asp:RegularExpressionValidator>

<Button runat="server" ID="btnSubmit" Text="Update" ValidationGroup="smryUpdate" Width="100px" OnClick="btnSubmit_Click" >

Useful Sources:

Note:

If validation doesn’t work on client-side, try adding these lines into web.config:

<configuration>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
</configuration>

Ned Sahin

Blogger for 20 years. Former Microsoft Engineer. Author of six books. I love creating helpful content and sharing with the world. Reach me out for any questions or feedback.

Leave a Comment