开发者

adding regular expression jquery

开发者 https://www.devze.com 2023-04-12 13:24 出处:网络
hi i am using jquery validation plugin , i want to add a validation to my price field , the only available are numbers 0-9 , comma , dot

hi i am using jquery validation plugin , i want to add a validation to my price field ,

the only available are numbers 0-9 , comma , dot

1000
1000.00
1,000.00

should be available please help . this is my function

    var x=$("#form").validate({                     

        rules: {
            price: {
                custom_number: true
            }
        },

        messages: {

        }

    }); 

$.validator.addMethod("custom_number", 开发者_如何学JAVAfunction(value, element) {
    return this.optional(element) || value === "NA" ||
        value.match(/^[0-9,\+-]+$/);
}, "Please enter a valid number");

i want to add my expression here value.match(/^[0-9,\+-]+$/)

thanks...............................


Here is the RegExp that should work:

^[1-9]\d{0,2}(?:\d+|(?:,\d{3})*)(?:\.\d{2})?$

Here you can test it against various strings.


This one should do it:

value.match(/^[0-9,.]+$/);


Believe it or not, this behemoth seems to work, and it permits the dollar sign.

^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$

I borrowed it from RegExLib.com. If this regex doesn’t work for you, perhaps one of the others will.

0

精彩评论

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

关注公众号