javascript - Extracting validation logic into a method for reuse? -


i using jquery validation plugin , working fine. have validation logic duplicated in 2 places 1 on form submit , other on focus out. trying extract method , reuse whereever required. trying below.

$(document).ready(function() {       validateform();        function validateform(){             $("#myform").validate({               onfocusout: function(element) { jquery(element).valid(); } ,               rules:{                 attribute: "required"             },             messages:{                 attribute: "required field"             }         });        }      $('#myform #submit').click(function(e) {              e.preventdefault();       var validator = validateform();  //here shows error line      if(validator.form()){            //do         } });   }); 

if extract logic above getting script error below.

error: 'undefined' null or not object 

do need return method?

thanks!

i don't have experience validation plugin, can tell reason getting reference error because validateform() returns undefined automatically when finishes executing.

i'm guessing form() method of object $().validate() returns, if need make validateform() return result of validate() so

function validateform(){     return $("#myform").validate({ /* options */ }); } 

good luck!

edit: also, why invoking validateform() on document ready - surely form whatever in markup @ point...


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -