Validate phone number : RegularExpressionValidator « Validation « ASP.NET Tutorial






<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Regular Expression Tester</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        Phone Number:<br />
        <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
           <asp:RegularExpressionValidator 
           ID="regPhone" 
           runat="server" 
           ControlToValidate="txtPhone" 
           ValidationExpression="^(\(?\s*\d{3}\s*[\)\-\.]?\s*)?[2-9]\d{2}\s*[\-\.]\s*\d{4}$"
           Text="Enter a valid phone number" />   
        
         
      <asp:Button ID="btnSubmit" 
                  Text="Click this to test validation" 
                  runat="server" />
      
    </div>
    </form>
</body>
</html>








8.10.RegularExpressionValidator
8.10.1.Making sure the text-box value is an e-mail address
8.10.2.Validate phone number
8.10.3.Validate email
8.10.4.Validate URL
8.10.5.Validate Name
8.10.6.Validate Zip code