In a web form there are different asp.net validation controls. Is it possible to check a particular validation control is valid ? For example on leaving focus of textbox, first I will check requiredFieldValidatorUserName is valid ? If it is valid then I will check on server using ajax that this user name is not booked already.
Edit:
Explaination: I want to check validity (that input was valid) of a validation control on client side.
Please guide.
The best way would be to use a
CustomValidatorwith client side code, as this will display all the error messages, block form submission and also ensure that the validation is repeated at the server side – remember, just because you have client-side validation available, doesn’t mean the user’s seen it: Always validate your input at the server as well.Your
CustomValidatorwould then be coded to call the Ajax methods, and would show the error messages correctly to the client:And your ClientValidationFunction should look something like:
(Obviously, you’ll need to write the ajaxCallUserNameTaken method to call your page method/web service/etc.)
Doing it this way will ensure that the validation methods happen as expected; this will get called whenever the user tabs out of the textbox leaving a value (it won’t get called if the textbox is empty), and will ensure that the user can’t submit the page until they supply a unique value. You’ll also want to create the method referenced in
OnServerValidateto ensure that the value’s good once it hits the server too – this should call the same code that the AJAX endpoint uses to reduce duplication of code, etc.I was originally going to suggest that you could use the
Page_Validatorsobject on the client-side to do some checking in theonBlurevent, but I don’t really think this is suitable here as it results in more pain:OnBlurif a user moves out of a control without setting a value – only if they set and clear the value, so even ifisvalidistrue, you need to check for an empty string!