Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Custom Validation query for Money

Tags:

jquery

regex

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 (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.");

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.

like image 934
TheTodd Avatar asked May 23 '11 18:05

TheTodd


2 Answers

just strip out the value before you validate. it's pretty simple:

var str = [your value];
str = str.replace('$','');
like image 163
Jason Avatar answered Nov 19 '22 19:11

Jason


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.

\$?

like image 2
BZink Avatar answered Nov 19 '22 19:11

BZink