Tuesday, June 18, 2013

Validation Controls in ASP.NET

Validation Controls

 ü  Common property for all validation controls is
a.    ErrorMessage
b.   ControlToValidate
 ü  We can alse set validation group for validation controls
          Eg: -checkavailabilty button only for username text box.
We can set validation group for that button and validation control for that text box.

1.RequiredFieldValidator
a.InitialValue  (eg :-  “—select—“ for dropdownlist)
2.   RegularExpressionValidator
a.      ValidationExpression ( for email,length of password etc…)
Eg: ([A-Za-z0-9]{8,15}) – for password strength.
    Or ---  ^([A-Za-z0-9]{8,15})$
3.   CompareValidator
a.      ControlToValidate
b.      Control to Compare
4.   RangeValidator
a.      Minimum value
b.      Maximum value
c.       Type ( eg: for age between 20-30 – type –integer)
5.   ValidationSummary
a.      To show summary in separate section;
6.   CustomValidator  (takes values from server )
a.      Control to validate
b.      Error Message
c.       Write Code on CustomValidator Click
Example : - for SalaryField ( between 5000-8000)

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            int val = Convert.ToInt32(args.Value);
            if (val>=5000 && val <=8000)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
        catch
        {
            args.IsValid = false;
        }

}

No comments:

Post a Comment