Monday, September 18, 2006

Working with validators in ASP.NET

Working with validators

 

In any application, validating the user input is very important; it is process of the identifying what is incorrect in the user inputs. It is used to identify the errors in the user input before carrying out the business process.

 

Take for example; we are going to create a new email-using Yahoo. Where in the user id has to be given before submitting the form. This is one kind of validation. There are several other cases like the password should not exceed 10 characters or Date of birth entered should not exceed the current date or the age should be in the between a range say (10 to 50 or above 10 etc) or even the format in which the data are given.

  

Validators in ASP.NET

 

In this article lets discuss about the validators and its kinds in ASP.Net.

 

There are five different kinds of validators available in .net, they are as follows,

 

  • Required Field validators
  • Compare Validators
  • Range Validators
  • RegularExpression validators
  • Custom Validators

 

Let us first see what each validators is meant for.

 

Required Field Validators

 

The name itself describes it, yes it is the validator used to check whether the user has entered/selected a value or not.

 

[code]

<asp:RequiredFieldValidator id="valReq" runat="server" ControlToValidate=”"
    ErrorMessage="* You must enter a value" Display="dynamic">

</asp:RequiredFieldValidator>

[/code]

 

Compare Validators

 

In some case we may have to do a validation based on some of the underlying values/control data, for example.,

 

Now we need to check whether the Password and Re-Enter password in the new user form are the same. In these cases compare validators can be used.

 

[code]
<asp:CompareValidator id="valCom" runat="server"
    ControlToValidate="textbox1" ControlToCompare="textbox2"
    Operator="Equals"
    ErrorMessage=”"
    Display="dynamic">*

</asp:CompareValidator>[/code]  

Range Validator Control

 

In most of the case we may have to check whether the data entered is between a specified range. For example, the age should be minimum 10 and maximum 100.

 

In those cases range validators can be used.

 

[code]

<asp:RangeValidator id="valRange" runat="server"
    ControlToValidate="age"
    MaximumValue="100"
    MinimumValue="10"
    Type="integer"

    ErrorMessage=“* The age must be between 10 and 100″ Display=“static”></asp:RangeValidator> [/code] 

Regular Expression validator

 

It is one of important validators available in Asp.net it is used to compare the data entered against specific format. For example date has to be entered in dd/MM/yyyy format only.

 

[code]

<asp:RegularExpressionValidator id="valReg" runat="server"
    ControlToValidate=""
    ValidationExpression=""
    ErrorMessage=""
    display="dynamic">

</asp:RegularExpressionValidator>

[/code]

 
Custom Validators 

In all the cases the above validators cannot be used, that is some validations cannot be done on the page, it has to come for round trips to the server validate the data in the server against some condition or some data base values and then display error messages based on that.

 

For example, checking whether the user name already exists or not.

 

<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage=" " dispaly="dynamic">*

</asp:CustomValidator>

 

We will see in depth of these validators in our next chapters.

 

Now lets see what are the attributes are methods available in common

 Common Properties and Methods of validators. 

ControlToValidate –

 

Take a string input, name of the control to be validated.

 

ErrorMessage

 

The error message that needs to be displayed in case if the validations fails (some error in the data)

 

IsValid

 

Returns true if the control has valid date, else returns false.

 

Validate

 

Method to validate the control against the validation rule and to update the Isvalid property.

 

Display

 

There are three different ways of presenting the data or error message to the user.

 

  • None – Validation message will not be displayed at all
  • Static – Allocate space on the page to display the error message.
  • Dynamic – Space will not be allocated on the page if there is no error. Space will be dynamically added only when the validation fails.

  

Posted by Sadha at 12:28:12

Working with validators in ASP.NET

Working with validators

 

In any application, validating the user input is very important; it is process of the identifying what is incorrect in the user inputs. It is used to identify the errors in the user input before carrying out the business process.

 

Take for example; we are going to create a new email-using Yahoo. Where in the user id has to be given before submitting the form. This is one kind of validation. There are several other cases like the password should not exceed 10 characters or Date of birth entered should not exceed the current date or the age should be in the between a range say (10 to 50 or above 10 etc) or even the format in which the data are given.

  

Validators in ASP.NET

 

In this article lets discuss about the validators and its kinds in ASP.Net.

 

There are five different kinds of validators available in .net, they are as follows,

 

  • Required Field validators
  • Compare Validators
  • Range Validators
  • RegularExpression validators
  • Custom Validators

 

Let us first see what each validators is meant for.

 

Required Field Validators

 

The name itself describes it, yes it is the validator used to check whether the user has entered/selected a value or not.

 

[code]

<asp:RequiredFieldValidator id="valReq" runat="server" ControlToValidate=”"
    ErrorMessage="* You must enter a value" Display="dynamic">

</asp:RequiredFieldValidator>

[/code]

 

Compare Validators

 

In some case we may have to do a validation based on some of the underlying values/control data, for example.,

 

Now we need to check whether the Password and Re-Enter password in the new user form are the same. In these cases compare validators can be used.

 

[code]
<asp:CompareValidator id="valCom" runat="server"
    ControlToValidate="textbox1" ControlToCompare="textbox2"
    Operator="Equals"
    ErrorMessage=”"
    Display="dynamic">*

</asp:CompareValidator>[/code]  

Range Validator Control

 

In most of the case we may have to check whether the data entered is between a specified range. For example, the age should be minimum 10 and maximum 100.

 

In those cases range validators can be used.

 

[code]

<asp:RangeValidator id="valRange" runat="server"
    ControlToValidate="age"
    MaximumValue="100"
    MinimumValue="10"
    Type="integer"

    ErrorMessage=“* The age must be between 10 and 100″ Display=“static”></asp:RangeValidator> [/code] 

Regular Expression validator

 

It is one of important validators available in Asp.net it is used to compare the data entered against specific format. For example date has to be entered in dd/MM/yyyy format only.

 

[code]

<asp:RegularExpressionValidator id="valReg" runat="server"
    ControlToValidate=""
    ValidationExpression=""
    ErrorMessage=""
    display="dynamic">

</asp:RegularExpressionValidator>

[/code]

 
Custom Validators 

In all the cases the above validators cannot be used, that is some validations cannot be done on the page, it has to come for round trips to the server validate the data in the server against some condition or some data base values and then display error messages based on that.

 

For example, checking whether the user name already exists or not.

 

<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage=" " dispaly="dynamic">*

</asp:CustomValidator>

 

We will see in depth of these validators in our next chapters.

 

Now lets see what are the attributes are methods available in common

 Common Properties and Methods of validators. 

ControlToValidate –

 

Take a string input, name of the control to be validated.

 

ErrorMessage

 

The error message that needs to be displayed in case if the validations fails (some error in the data)

 

IsValid

 

Returns true if the control has valid date, else returns false.

 

Validate

 

Method to validate the control against the validation rule and to update the Isvalid property.

 

Display

 

There are three different ways of presenting the data or error message to the user.

 

  • None – Validation message will not be displayed at all
  • Static – Allocate space on the page to display the error message.
  • Dynamic – Space will not be allocated on the page if there is no error. Space will be dynamically added only when the validation fails.

  

Posted by Sadha at 12:27:28
Comments

No Responses to “Working with validators in ASP.NET”

  1. i think it is better if you can write more.

Leave a Reply