views:

38

answers:

1

I've seen a lot of examples on how to use message: to have custom error text. What I'm trying to achieve is something different.

I have client-side and server-side validation, so I want the same error message shown by both. To avoid duplication of code, it seems like having <div class="error" id="bad_email">Invalid e-mail</span> is the best approach.

I can't find a way to do this in the Validation jQuery plugin. Any ideas?

+1  A: 

There is a way to do it, you should read the documentation of the plugin. Read it from the link here

Here is how to do it.

Use the invalidHandler.

$(".selector").validate({
    invalidHandler: function(form, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = errors == 1
          ? 'You missed 1 field. It has been highlighted'
          : 'You missed ' + errors + ' fields. They have been highlighted';
        /* .... put here the call to a custom function to display validation error */
        customvalidationdisplay(message,"show");

      } else {
        /* ... put here the call to a custom function to hide validation error if displayed */
          customvalidationdisplay("hide");
      }
    }
 });


function customvalidationdisplay(message,action){
/* write here your custom script to display the error if action = "show" or hide if action = "hide" */

}

I hope you have understood this by now. If not, feel free to ask me for more information.

Raf
I did see this as an example, but it's still not quite what I was looking for. I have several error-divs, one for each type of error. Does the validator object contain a list of the failed checks? I couldn't find a description of the validator object in the documentation.
Stephon Striplin
Can you clarify me what you mean to say in your question "I have client-side and server-side validation, so I want the same error message shown by both", I am confused in this part. Cause, in jquery validation plugin, it will validate the form before it gets submitted to the server. Or do you mean to say, you want to validate a data in a field by requesting a serverside validation? such as checking for duplicate email before submitting the form?
Raf
Did you check the link I have given in my answer, and did you click the "options" link in that page which will show the detail options. You can find submitHandler, invalidHandler, etc. From these options, there is also an option called "groups" which uses errorPlacement to control where the group message is placed for list of items to be validated as a group.
Raf
If you still have problem, can you copy paste the codes in your question, this will help me understand the actual task.
Raf