Reputation: 1723
Yes, yet another question on regex with the JQuery validation plugin. I've followed all the examples I've found but for some reason cannot get this to validate only regex matches. It validates everything regardless of what regex I use (going for dollar amount under 100, optional $ and optional decimal up to hundredths place):
$.validator.addMethod("regexp", function (value, element) {
return this.optional(element) || /^\$?\d{0,2}(\.\d{1,2}$)?/.test(value);
}, 'Please enter a valid USD format under $100, dollar sign optional.');
$("#formname").validate({
debug: true,
rules: {
textbox: {
required: true,
regexp: true
}
}
});
Upvotes: 1
Views: 4810