I am trying to clean up some of the JavaScript throughout my views and one of the things I would like to do is move my jQuery Validation code to an external script function and pass in references to the fields I need to process/validate. The issue I am facing is pertaining to how the signature of jQuery Validate's rules field is formatted:
$("#form").validate({
rules: {
txtNoSpam: {
remote: WebSettings.SpamFilterValidationUrl
}
},
messages: {
txtNoSpam: {
remote: "Answer is incorrect."
}
},
});
In the above code 'txtNoSpam' maps directly to an element on my page called txtNoSpam, but I would much rather pass txtNoSpam into my initialization function as an object and then map the validation to the correct field using the supplied object's element name as the parameter name:
function Init(form, field1)
form.validate({
rules: {
field1.attr('name'): {
remote: WebSettings.SpamFilterValidationUrl
}
},
messages: {
field1.attr('name'): {
remote: "Answer is incorrect."
}
},
});
}
Is there any way I can achieve this?