jQuery form validation: why validates two times? -


i put test code on jsfiddle. please check here

when hit submit button why 2 times alert message?

you misusing validate function , andrewr aluded to. there many many things change how many times validating function called. in example, "rule" (which function in case) being called on:

  • text1 loses focus (if value has changed)
  • submit button clicked
  • form submitted
  • form loaded

this correct validation show errors on field edited when move off number of other events.

if want perform functionality on validation of specific field, better practice add onvalidating callback run custom methods field. rules should rules , not methods, that's callbacks (and yes, callback executed multiple times because many events fire validate method).

if need fire once , on form submit, disable default validation behaviour , add manual validate() call following:

$("#test1").submit(function() {    $("#test1").validate(); }); 

flat out @ moment, can pop , provide example of if need it.

ps: famous way double events firing binding event twice. if you're not sure how pages initialised, it's safer "unbind" event before bind it. not problem in case, might come try fix problem , can cause confusion - has me in past :)


Comments