Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET Phone Validation Using RegularExpressionValidator

ASP.NET RegularExpressionValidator is very usefull in phone and mobile number validation. The RegularExpressionValidator control belongs to System.Web.UI.WebControls

Syntax

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" 
ControlToValidate="TextBox3" ErrorMessage="Not a Valid Phone#." 
ValidationExpression="^(\(?\s*\d{3}\s*[\)\-\.]?\s*)?[2-9]\d{2}\s*[\-\.]\s*\d{4}$"></asp:RegularExpressionValidator>

ASP.NET RegularExpressionValidator Control Property

PropertyDescription
ControlToValidateSpecifies control name which needs to be validate.
DisplaySpecifies behavior of the error message.
EnableClientScriptSpecifies whether or not client-side validation is enabled.
EnabledSpecifies whether or not validation control is enabled or not.
ErrorMessageSpecifies error messageto be display in validation summary.
ForeColorSpecifies error message color.
IsValidIndicate if associate control passes validation or not.
SetFocusOnErrorSet focus on associate control if validation fails.
TextSpecifies text to be display if validation fails.
ValidationGroupSpecifies validation group name.
ValidateUpdate the isvalid propery.
ValidateGetValidationPropertyDetermineing validation property of a control, if it exists.
ValidationExpressionSpecifies regular expression that you want to match against input value.

ASP.NET Phone Validation Using RegularExpressionValidator Example

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.NET Phone Number Validation Example</title>
</head>
<body>
<form id="Form" runat="server">
<div style="width: 619px">
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium" 
Font-Underline="True" Text="ASP.NET Phone Validation Example"></asp:Label><br/><br/>
<table border="0" width="600px" cellpadding="0" cellspacing="0">
<tr><td>
<asp:Label ID="lblPhone" runat="server" Font-Bold="True" Font-Size="Small" 
Text="Enter Phone#:"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Width="177px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" 
ControlToValidate="TextBox3" ErrorMessage="Not a Valid Phone#." 
ValidationExpression="^(\(?\s*\d{3}\s*[\)\-\.]?\s*)?[2-9]\d{2}\s*[\-\.]\s*\d{4}$"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
ControlToValidate="TextBox3" ErrorMessage="This is required field, can not be blank."></asp:RequiredFieldValidator>
</td></tr>
<tr><td>
<asp:Button ID="btnSubmit" runat="server" Font-Bold="True" 
Font-Size="Small" Text="Submit" Width="60px" />
</td></tr>
</table>   
</div> 
</form>
</body>
</html>

Above example will produce following output

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary