How can you do validations in Ajax?

 Posted by ArticlesMaint on 10/1/2009 | Category: ASP.NET AJAX Interview questions | Views: 5986

We can perform all the necessary validation like required field, type checking, range checking etc using Ajax. Below is a small snippet which shows how to use a required field validator. We have numbered the code snippet to understand the code more proper.

  • 1 -> We have defined a text box ‘TextBox1’ which will be validated for required field.

  • 2 -> We have defined a simple ‘<span>’ HTML tag which will display the error message.

  • 3, 4 and 5 -> We use the XML declarative Ajax script to define that ‘TextBox1’ has validators and it’s a required field validator. To define the required field validator we need the ‘RequiredFieldValidator’ controls inside the validators.

  • 6 -> We then define where the error should be displayed using the ‘ValidationErrorLabel’. In this case we will be displaying error in the span ‘Validator1’ which was defined previously.

Figure 18.15:- Validations in Ajax

Note: - The above sample shows a sample for 'requiredFieldValidator' , but we can also use other validators like rangeValidator, typeValidator, rangeValidator and regexValidator. As this is a interview book we will not be getting in to details of the other validators, please feel free to practice some sample for the same.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response