Home > ASP.Net > Validate Age not to be 15 year

Validate Age not to be 15 year


Hi

try this example:

<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>

<asp:CompareValidator ID="cfvDOB" ControlToValidate="txtDOB" Type="Date" Operator="LessThan"
     ErrorMessage="(DoB cannot be within the last 15 years)" Display="Dynamic" SkinID="fielderror"
     runat="server"></asp:CompareValidator>

in code behind :-

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cfvDOB.ValueToCompare = DateTime.Now.AddYears(-15).ToString("d");            
        }
    }

//———-Or————-

<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>

<asp:RangeValidator id="valDateRange" runat="server"  ControlToValidate="txtDOB" Type =Date Display="Dynamic"
     ErrorMessage="(DoB cannot be within the last 15 years)"/>

in code behind :-

void Page_laod(object sender, EventArgs e)
{

RangeValidator1.MinimumValue = DateTime.Today.AddYears(-15).ToShortDateString();
RangeValidator1.MaximumValue = DateTime.Today.ToShortDateString();

}

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: