Priyank Patel
Priyank Patel

Reputation: 6996

Asp.net Custom Validator

I am performing both clientside validation and server side validation using jquery and Asp.net custom validator, required field validator.What i am doing is i display a 'X' next to the tetxbox which has wrong input.This is working fine , my issue is when a textbox has wrong input it shows 'X' as jquery validation function fires it , at the same time when he submits the data , custom validator also displays a 'X', so two 'X''X' are displayed like this, This kind of looks odd . So what can i do so that it is displayed properly , should i remove text from custom validator and only show error message on validation summary.any suggestions would be appreciated. Thanks

Upvotes: 2

Views: 814

Answers (2)

Robbie
Robbie

Reputation: 19490

The CustomValidator control has a property called ClientValidationFunction. This allows you to call a javascript method that has only one job - to set the IsValid property (of the args parameter) to true or false. The custom validator will take care of the error message for you. There is no need for you to have any jquery for showing and hiding the error. It should just work. Asp.net places some javascript on the page for this purpose so you don't have to.

Note: If you have a second RequireFieldValidator (or any other validator) for the same input, then be aware that you should set the Display="Dynamic" property which will ensure that the validator does not consume screen space while it is not being shown (which would cause your other validators messages to be pushed out of position).

For more details of the custom validators ClientValidationFunction property, see this page

I hope this helps

Upvotes: 3

utsikko
utsikko

Reputation: 1545

In my opinion keep using both, but just don't show 'X' on your jQuery validation. The Text attribute in the Custom Validator, will be seen in any case when submitting, and the ErrorMessage attribute on that validator, will appear in the summary. I have the same situation, and it works fine.

Hopefully, it helps you.

Upvotes: 3

Related Questions