jQuery Custom Validation query for Money
I have this for a custom validation for a text box that accepts money:
//custom validator for money fields
$.validator.addMethod("money", function (value, element) {
return this.optional(element) || value.match(/^\$?\d+(\.(\d{2}))?$/);
}, "Please provide a valid dollar amount (up to 2 decimal places) and do not include a dollar sign.");
It seems to work, but I don't want the dollar signs allowed. Do I change it to this:
//custom validator for money fields
$.validator.addMethod("money", function (va开发者_如何学JAVAlue, element) {
return this.optional(element) || value.match(/^\?\d+(\.(\d{2}))?/);
}, "Please provide a valid dollar amount (up to 2 decimal places) and do not include a dollar sign.");
Or should I just strip out the dollar sign somewhere else and not bother the user with such a trivial problem? If that's true, where should I do that?
Thanks for answering such a n00b question.
just strip out the value before you validate. it's pretty simple:
var str = [your value];
str = str.replace('$','');
No, you would change it to this..
value.match(/^\d+(.(\d{2}))?/)
These 3 characters made up the part of the regex that matched the dollar sign.
\$?
精彩评论