Validate input of US SSN : Regular Expression Validator « Validation by Control « ASP.Net






Validate input of US SSN


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label5" 
                   runat="server" 
                   Font-Bold="True" 
                   Font-Size="Large" 
                   Text="ASP.NET Validators"/>
        <asp:Label ID="Label1" 
                   runat="server" 
                   Font-Bold="True" 
                   Text="Required Field:"/>
        <asp:TextBox ID="txtRequiredField" 
                     runat="server">Name</asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                                    runat="server" 
                                    ControlToValidate="txtRequiredField"
                                    ErrorMessage="No data." 
                                    InitialValue="Please enter your name"/>
        <asp:Label ID="Label2" 
                   runat="server" 
                   Font-Bold="True" 
                   Text="Range 0 - 100:"/>
        <asp:TextBox ID="txtRange" runat="server"/>
        <asp:RangeValidator ID="RangeValidator1" 
                            runat="server" 
                            ControlToValidate="txtRange"
                            ErrorMessage="Should be between 0 and 100." 
                            MaximumValue="100" 
                            MinimumValue="0"
                            Type="Integer"/>
         <asp:Label ID="Label3" 
                    runat="server" 
                    Font-Bold="True" 
                    Text="Regular Expression:"/>
         <asp:TextBox ID="txtRegExp" runat="server"></asp:TextBox>
         <asp:RegularExpressionValidator 
                    ID="RegularExpressionValidator1" 
                    runat="server" 
                    ControlToValidate="txtRegExp"
                    ErrorMessage="Please enter a valid US SSN." 
                    ValidationExpression="\d{3}-\d{2}-\d{4}"/>
         <asp:Label ID="Label4" 
                    runat="server" 
                    Font-Bold="True" 
                    Text="Value < 20"/>
         <asp:TextBox ID="txtComparison" runat="server"></asp:TextBox>
         <asp:CompareValidator 
                    ID="CompareValidator1" 
                    runat="server" 
                    ControlToValidate="txtComparison"
                    ErrorMessage="Enter a value less than 20." 
                    Operator="LessThan" 
                    ValueToCompare="20"/>
         <asp:Button ID="btnPostback" runat="server" OnClick="btnPostback_Click" Text="Post back" />
         <asp:Label ID="lblValidationComplete" runat="server"/>
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{

    protected void btnPostback_Click(object sender, EventArgs e)
    {
        lblValidationComplete.Text = "You passed validation!";
    }
}

 








Related examples in the same category

1.Try validation based on Regular Expression in C#
2.Using a RegularExpressionValidator Control to Prevent the Entry of a Special Character (VB.net)
3.Regular expression validator: ^\d{5}$|^\d{5}-\d{4}$ (VB.net)
4.A valid 5 digit zip code