0

I am trying to display jquery validation errors as a title. i.e., any validation errors we are displaying it as a title, which is working fine.

I am adding the title to the element in placeError function.

As we know jquery does eager validation and for email field when data is not entered, it displays as email field is mandatory the moment data is being entered if its invalid format, it displays as "Invalid format" (this happens only when i display the error message in div or span ) i need this behavior to be applied on element title also, can some one shed some light on how to handle this, where can i handle this.

If the above information is not sufficient, i can provide more details.

Pramod CA
  • 47
  • 3
  • 11
  • Can you post a demo on http://jsfiddle.net ? – Abraham Aug 30 '12 at 10:15
  • @Abraham Please try this demo(which is similar to what i am trying to acheieve, in this demo there is inline valdiation, where as i have added rules and i am using errorPlacement) [link](http://jquery.bassistance.de/validate/demo/ ) Dont enter any details and submit, in the first part of the form, it says "This field is required." for Name, email and Your comment, now enter invalid email, than it displays "Please enter a valid email address." spontaneously. I need a handler, when it says "Please enter a valid email address." – Pramod CA Aug 30 '12 at 10:38
  • Check this - `errorPlacement` is getting called: http://jsbin.com/avuhog/3/edit – Abraham Aug 30 '12 at 10:44
  • @Abraham 'errorPlacment' is called on submit, what i am looking is for an handler at the time of eager validation. Please see eager validation behavior in the demo which i have shared. – Pramod CA Aug 30 '12 at 10:53
  • 1
    `errorPlacement` is called in my demo every time the alert box pops up (on keypress when the field is invalid). – Abraham Aug 30 '12 at 10:54
  • @Abraham Thanks a lot , it has solved the purpose.Is there any thing that i should do to say that you have answered, currenly i have voted your answer. – Pramod CA Aug 30 '12 at 11:13

1 Answers1

0

Use the errorPlacement callback; like:

$(yourForm).validate({
    // some stuff here
errorPlacement: function(error, element) {
    // do stuff with error and element
        }
    // some more stuff here
});

see Jquery Validation plug-in custom error placement

Community
  • 1
  • 1
Vishal
  • 1,236
  • 1
  • 9
  • 16
  • i do have the errorplacement, here is where i am adding title for the element when it has validation errors, which is working fine. Only for the scenario, where email is not entered, it says "Email is mandatory" when i enter invalid email than it says "Invalid email" as the message, at this stage control doesn't come to 'errorplacement' ,this is where i am facing the issue, if i can get hold of place where it triggers "Invalid Email" i belive i can catch the message and add it as title – Pramod CA Aug 30 '12 at 10:15
  • I think you could check that too inside `errorPlacement` like `if ($(error).text() == 'Invalid email') { // do some stuff }`. – Vishal Aug 30 '12 at 10:29
  • I have an alert statement there, its not invoking that when it says "Invalid Email", which hints that its not calling errorPlacement? (Note its invoking when it says "Email is required", hence errorPlacement is called) – Pramod CA Aug 30 '12 at 10:33
  • thanks for the input, i had declared errorplacement as a variable and i was using that variable name in the validate method, hence it was not invoking, when i have errorplacement as you have shown, it works, thanks. – Pramod CA Aug 30 '12 at 11:12