I have a textbox in which user enters the jobnumber. I am checking if this number exists in the database, by using a server side CustomValidator. I want this CustomValidator to be called before anything else in the page. Right now it is firing the CustomValidator only if all the RequiredFieldValidators are validated to true. And the validation is happening on button click.
Is it possible to validate CustomValidator before the other RequiredFieldValidators? Also, is there a way in which, as soon as the jobnumber is entered into the textbox, we can validate the number and display an error if it is invalid, immediately and not wait until the button click?
答案 0 :(得分:0)
If you're using a CustomValidator
, then set the ValidateEmptyText
property to true and add code to your validation method to check if the value is populated. Then you don't need the RequiredFieldValidator
at all. Just make sure you have a JavaScript function to do the validation client-side and set the ClientValidationFunction
property.
To trigger the validation whenever you want , you can use the method described here (although I've never tried it): http://fczaja.blogspot.ca/2009/07/aspnet-how-to-trigger-client-side.html
function Validate()
{
// Get the specific validator element
var validator = document.getElementById('RequiredFieldValidator1');
// Validate chosen validator
ValidatorValidate(validator);
// Update validation summary for chosen validation group
ValidatorUpdateIsValid();
ValidationSummaryOnSubmit(validationGroup);
}
Then you can use that in the keypress
event of the textbox.