I'm trying create a generic regex validator to use in a multi-input form which needs different rules for the individual input fields (also depending on other options selected).
I had this more or less working with vanilla JavaScript, but read that it would be simpler with jQuery. I found this code on another site, but I just can't get it to work.
Could someone please either point out what might be wrong with the code, or give me an example of how it could be used?
I'm new to programming in general so sorry if this is 'easy'.
Here is the code:
$.validator.addMethod('regexp', function(value, element, param) {
return this.optional(element) || value.match(param);
},
'This value doesn\'t match the acceptable pattern.');
$('form').validate({
rules: {
password: {
required: true,
regexp: /^[A-Za-z\d]+$/i
}
}
});