- What are ASP.NET Validation controls?ASP.NET provides validation controls to help you check Web form data entries before the data is accepted and saved in the Database. Validation controls can be used to address the following questions.
1. Did the user enter anything?
2. Is the entry the appropriate kind of data (For example, Date of Birth should be a valid Date, Name should be a string etc.)?
3. Is the data within a required range?(For example age cannot be greater than 100 years)
The validation controls check the validity of data entered in associated server controls on the client before the page is posted back to the server.Most validity problems can be caught and corrected by the user without a round-trip to the server. - Where do the ASP.NET validation controls validate data, on the Client or on the Web Server?
ASP.NET validation controls validate data first on the client and then on the web server. If a client disables javascript on the browser then, client side validations are bypassed and validations are performed on the web server.
Client-side validation is provided by a JScript library named WebUIValidation.js, which is downloaded separately to the client. Validation controls also automatically provide server-side validation. Server-side validation is always performed, whether or not client-side validation has occurred. This double-checking ensures that custom validations are performed correctly and that client-side validation has not been circumvented. - What are the 6 different validation controls provided by ASP.NET?
RequiredFieldValidator:Checks whether a control contains data
CompareValidator:Checks whether an entered item matches an entry in another control
RangeValidator:Checks whether an entered item is between two values
RegularExpressionValidator:Checks whether an entered item matches a specified format
CustomValidator:Checks the validity of an entered item using a client-side script or a server-side code, or both
ValidationSummary:Displays validation errors in a central location or display a general validation error description
Validation Control
|
Description
|
RequiredFieldValidation
|
Makes an input
control a required field
|
CompareValidator
|
Compares the value
of one input control to the value of another input control or to a fixed
value
|
RangeValidator
|
Checks that the user
enters a value that falls between two values
|
RegularExpressionValidator
|
Ensures that the
value of an input control matches a specified pattern
|
CustomValidator
|
Allows you to write
a method to handle the validation of the value entered
|
ValidationSummary
|
Displays a report of
all validation errors occurred in a Web page
|
- What property of the validation control is used to specify which control to validate?
ControlToValidate property. - Explain in simple steps how to use validation controls?
1.Draw a validation control on a Web form and set its ControlToValidate property to the control you want to validate.
2.If you’re using the CompareValidator control, you also need to specify the ControlToCompare property.
3.Set the validation control’s ErrorMessage property to the error message you want displayed if the control’s data is not valid.
4.Set the validation control’s Text property if you want the validation control to display a message other than the message in the ErrorMessage property when an error occurs. Setting the Text property lets you briefly indicate where the error occurred on the form and display the longer ErrorMessage property in a ValidationSummary control.
5.Draw a ValidationSummary control on the Web form to display the error messages from the validation controls in one place.
6.Provide a control that triggers a postback event. Although validation occurs on the client side, validation doesn’t start until a postback is requested. - Are the validation controls fired on the client side if javascript is disabled on the client browser?
No, validation controls are not fired on the client side if javascript is disabled on the client browser. - What is the use of CausesValidation property of an ASP.NET button control?
CausesValidation property of an ASP.NET button control is used to determine if the validation controls should be fired when the button is clicked. If CausesValidation property is set to true, then validation is performed and if the CausesValidation property is set to false then validation is not done. - Why we use validation controls?
Validation is important part of any web application. User's input must always be validated before sending across different layers of the application.
Validation controls are used to:
- Implement presentation logic.
- To validate user input data.
- Data format, data type and data range is used for validation.
- Validation is of two types:
- Client Side
- Serve Side
Client side validation is good but we have to be dependent on browser and scripting language support.
Client side validation is considered convenient for users as they get instant feedback. The main advantage is that it prevents a page from being postback to the server until the client validation is executed successfully.
For developer point of view serve side is preferable because it will not fail, it is not dependent on browser and scripting language.
You can use ASP.NET validation, which will ensure client, and server validation. It work on both end; first it will work on client validation and than on server validation. At any cost server validation will work always whether client validation is executed or not. So you have a safety of validation check.
For client script .NET used JavaScript. WebUIValidation.js file is used for client validation by .NET
All validation
controls are rendered in form as <span> (label are referred as
<span> on client by server)
10. Important points for validation controls
10. Important points for validation controls
- ControlToValidate property is mandatory to all validate controls.
- One validation control will validate only one input control but multiple validate control can be assigned to a input control.
11. Validation Properties
Usually, Validation is invoked in response to user actions like clicking submit button or entering data. Suppose, you wish to perform validation on page when user clicks submit button.
Server validation will only performed when CauseValidation is set to true.
When the value of the CausesValidation property is set to true, you can also use the ValidationGroup property to specify the name of the validation group for which the Button control causes validation.
Page has a Validate() method. If it is true this methods is executed. Validate() executes each validation control.
To make this happen, simply set the CauseValidation property to true for submit button as shown below:
<asp:Button ID="Button2" runat="server" Text="Submit" CausesValidation=true />
Lets understand validation controls one by one with practical demonstration:
RequiredFieldValidation Control
The RequiredFieldValidator control is simple validation control, which checks to see if the data is entered for the input control. You can have a RequiredFieldValidator control for each form element on which you wish to enforce Mandatory Field rule.
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Style="top: 98px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
CompareValidator Control
The CompareValidator control allows you to make comparison to compare data entered in an input control with a constant value or a value in a different control.
It can most commonly be used when you need to confirm password entered by the user at the registration time. The data is always case sensitive.
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Style="top: 145px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
RangeValidator Control
The RangeValidator Server Control is another validator control, which checks to see if a control value is within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
<asp:RangeValidator ID="RangeValidator1" runat="server" Style="top: 194px; left: 365px;
position: absolute; height: 22px; width: 105px"
ErrorMessage="RangeValidator" ControlToValidate="TextBox4" MaximumValue="100"
MinimumValue="18" Type="Integer"></asp:RangeValidator>
RegularExpressionValidator Control
A regular expression is a powerful pattern matching language that can be used to identify simple and complex characters sequence that would otherwise require writing code to perform.
Using RegularExpressionValidator server control, you can check a user's input based on a pattern that you define using a regular expression.
It is used to validate complex expressions. These expressions can be phone number, email address, zip code and many more. Using Regular Expression Validator is very simple. Simply set the ValidationExpression property to any type of expression you want and it will validate it.
If you don't find your desired regular expression, you can create your custom one.
In the example I have checked the email id format:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Style="top: 234px;
left: 366px; position: absolute; height: 22px; width: 177px"
ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox5"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
ValidationSummary
ASP.NET has provided an additional control that complements the validator controls.
The ValidationSummary control is reporting control, which is used by the other validation controls on a page.
You can use this validation control to consolidate errors reporting for all the validation errors that occur on a page instead of leaving this up to each and every individual validation control.
The validation summary control will collect all the error messages of all the non-valid controls and put them in a tidy list.
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
style="top: 390px; left: 44px; position: absolute; height: 38px; width: 625px" />
ASP.NET has provided an additional control that complements the validator controls.
The ValidationSummary control is reporting control, which is used by the other validation controls on a page.
You can use this validation control to consolidate errors reporting for all the validation errors that occur on a page instead of leaving this up to each and every individual validation control.
The validation summary control will collect all the error messages of all the non-valid controls and put them in a tidy list.
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
style="top: 390px; left: 44px; position: absolute; height: 38px; width: 625px" />
No comments:
Post a Comment