开发者

jQuery Custom Validation query for Money

开发者 https://www.devze.com 2023-03-08 06:30 出处:网络
I have this for a custom validation for a text box that accepts money: //custom validator for money fields

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.

\$?

0

精彩评论

暂无评论...
验证码 换一张
取 消