Setting The Display Property Of Validator Controls
The Display property determines how the validation error message is rendered. This property accepts any of the following three possible values:-
1) Static
2) Dynamic
3) None
1) STATIC
By Default, the Display property has the value Static.
The validation error messagge is rendered by the validation cotnrol looks like this
Empty Field
2) DYNAMIC
But if, the Display property has the value Dynamic.
The validation error messagge is rendered by the validation cotnrol looks like this
Empty Field
Both the visibility and display attributes can be used to hide text in browser.
However, text hidden with the visibility attribute (Static) still occupies screen real estate.
On the other hand, text hidden with the display attribute (Dynamic) does not occupies screen real estate.
3) NONE
If you want to prevent the individual validation controls from displaying an error message and display the error message with a validation summary then we use None value for the display attribute
Labels: Validators
Code Snippet For How To Apply Validators In ASP.NET
As in my previous article i already mentioned how many types of validators in ASP.NET.
Today i will provide the code for applying validators in server control.
You just type the following code in the start and end tag of the body.
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
<form id="form1" runat="server">
<div>
Name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator" Text='<img src="caution.png" width="15" height="15"> Empty Name Field'></asp:RequiredFieldValidator>
<br />
Password
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="RequiredFieldValidator"
Text='<img src="caution.png" width="15" height="15"> Empty Password Field'
ControlToValidate="TextBox2"><img height="15" src="caution.png" width="15">
Empty Password Field</img></asp:RequiredFieldValidator>
<br />
Confirm Password
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="TextBox3" ErrorMessage="RequiredFieldValidator" Text='<img src="caution.png" width="15" height="15"> Empty Password Field'></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="CompareValidator"
Text='<img src="caution.png" width="15" height="15"> Password Does Not Match'
ControlToCompare="TextBox2" ControlToValidate="TextBox3"><img height="15"
src="caution.png" width="15"> Password Does Not Match</img></asp:CompareValidator>
<br />
Age
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="RequiredFieldValidator"
Text='<img src="caution.png" width="15" height="15"> Empty Age Field'
ControlToValidate="TextBox4"><img height="15" src="caution.png" width="15">
Empty Age Field</img></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="CompareValidator"
Text='<img src="caution.png" width="15" height="15"> Age Must Be Greater Than 18'
ControlToValidate="TextBox4" Operator="GreaterThan" Type="Integer"
ValueToCompare="18"><img height="15" src="caution.png" width="15"> Age Must
Be Greater Than 18</img></asp:CompareValidator>
<br />
Bonus
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="RequiredFieldValidator"
Text='<img src="caution.png" width="15" height="15"> Empty Bonus Field'
ControlToValidate="TextBox5"><img height="15" src="caution.png" width="15">
Empty Bonus Field</img></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="RangeValidator"
Text='<img src="caution.png" width="15" height="15"> Bonus Must Between 500 To 1000'
ControlToValidate="TextBox5" MaximumValue="1000" MinimumValue="500"
Type="Integer"><img height="15" src="caution.png" width="15"> Bonus Must
Between 500 To 1000</img></asp:RangeValidator>
<br />
Email-Id
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="RequiredFieldValidator"
Text='<img src="caution.png" width="15" height="15"> Empty Email-Id Field'
ControlToValidate="TextBox6"><img height="15" src="caution.png" width="15">
Empty Email-Id Field</img></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="RegularExpressionValidator"
Text='<img src="caution.png" width="15" height="15"> Invalid Email Format'
ControlToValidate="TextBox6"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"><img
height="15" src="caution.png" width="15"> Invalid Email Format</img></asp:RegularExpressionValidator>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
Labels: Validators
Validators in ASP.NET 4.0
Introduction:-
In asp.net we have numbers of validators with the help of it we can validate our data before it send to the server side. Thanks to Microsoft that provide validators for validing various tasks and reduce to write the javascript code that are used in earlier.
Now i would like to tell you about types of validators.
There are 6 types of validators that are provided by ASP.NET 4.0 :-
1) Required Field Validator
2) Compare Validator
3) Range Validator
4) Regular Expression Validator
5) Custom Validator
6) Validation Summary
Now talk about the tasks or work that each validator perform to fulfil our needs.
NOTE :- You must associate validator along with controls to perform validation . You can associate the control with validator to set the Property of Validator named ControlToValidate. Every validator has ControlToValidate property. You can applies more than one validator to a single control. You must also set the Text Property of the validator so that when the validator perform validation it display the text whatsoever you write in the validator's text property.
1) Required Field Validator :- It checks whether the field is empty or not.
2) Compare Validator :- We can compare controls with the help of compare validators (For eg. normally we compare the TextBox like PasswordTextBox and ConfirmTextBox) and also check the DataType of the control (means what kind of datatype value control can take).
3) Range Validator :- With the help of it we can make a check for min. and max. values.
4) Regular Expression Validator :- It has pre-defined format. It is basically used to check the format of the email-id or internet url.
5) Custom Validator :- When we want to make our own validators than we use custom validator. We have to write the javascript for the custom validator.
6) Validation Summary :- With the help of it we can display all validators summary at the end of the all controls.
Labels: Validators
