Extracting validation logic into a method for reuse?
I am using jquery validation plugin and is working fine. I have validation
logic duplicated in two places one is on form submit and the other is on
focus out. Now i am trying to extract it into a method and reuse it
whereever it is required. I am trying as 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 it shows error line
if(validator.form()){
//do something
}
});
});
If i extract the logic as above i am getting script error as below.
Error: 'Undefined' is null or not an object
Do i need to return anything from the method?
Thanks!
No comments:
Post a Comment