Interview Questions on ASP.NET Validation controls

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


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.

Give an example of real time scenario where you might use CausesValidation property of an ASP.NET button control?
Let us assume we have a Page that collects user information like name, age, date of birth, gender with a submit and reset buttons. When I click the submit button the information filled on the form should be validated and saved to the database. If I click the reset button then all the controls on the webform should default to their initial values without validation happening.So you have to set the CausesValidation property of the reset button to false for the validation to be bypassed. Other wise you will not be able to post back the page to the server.

What is ASP.NET Custom Validator used for?
ASP.NET Custom Validator is used to perform complex types of validation not provided by the standard validation control, use a CustomValidator control and write code to perform the validation on the server side and optionally on the client side.

How do you programatically check, if the client side validation is not bypassed by disabling the javascript on the client browser?
We use Page.IsValid property to determine if all the validations have succeeded. For this property to return true, all validation server controls in the current validation group must validate successfully.

How do you programatically invoke all validation controls on a page?
Call Page.Validate() method. When this method is invoked, it iterates through the validation controls contained in the ValidatorCollection object associated with the Page.Validators property and invokes the validation logic for each validation control in the current validation group.

What is a validation group?
Validation groups allow you to group validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.

How do you create a validation group?
You create a validation group by setting the ValidationGroup property to the same name for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.

Explain how a validation group works when the Page is posted by clicking a button?
During postback, the Page class's IsValid property is set based only on the validation controls in the current validation group. The current validation group is determined by the control that caused validation to occur. For example, if a button control with a validation group of LoginGroup is clicked, then the IsValid property will return true if all validation controls whose ValidationGroup property is set to LoginGroup are valid.

Can a DropDownList fire validation controls?
Yes, DropDownList control can also fire validation if the control's CausesValidation property is set to true and the AutoPostBack property is set to true.

How do you programatically force all validation controls in a particular validation group to be fired?
Call the Page.Validate(string GroupName) method and pass the name of the validation group. This will fire only the validation controls in that validation group.

What is SetFocusOnError property of a validation control used for?
Use the SetFocusOnError property to specify whether focus is automatically set to the control specified by the ControlToValidate property when this validation control fails. This allows the user to quickly update the appropriate control.

If multiple validation controls fail and this property is set to true, the control specified in the ControlToValidate property for the first validation control receives focus.

What is InitialValue property of a RequiredFieldValidator?
Use this property to specify the initial value of the input control.Validation fails only if the value of the associated input control matches this InitialValue upon losing focus.

10 comments:

  1. nice article appreciated for ur work

    ReplyDelete
  2. Very useful. Much appreciated. Please keep up the good work.

    ReplyDelete
  3. Very much helpfull ,this content is....

    ReplyDelete
  4. Thank you For such nice article.....

    ReplyDelete
  5. This type's of article's is really very useful for the freshers...thanks

    ReplyDelete
  6. thanks for this article, it helped me to recall all this in few min..

    ReplyDelete
  7. Sir,Nice Article,u r the Mascot for the programmer,,,,,,,,,Pls Keep it up ,,,

    ReplyDelete
    Replies
    1. Thank you very much for taking time to give feedback.

      Please subscribe to my channel using the link below, if you want to receive an email when I upload new videos.
      http://youtube.com/kudvenkat

      All .NET videos are organised into playlists at the following link. Hope you will find it useful.
      http://www.youtube.com/view_all_playlists

      Delete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us