简体   繁体   English

使用jquery验证器插件为十进制数设置正则表达式

[英]Set regex for decimal number with jquery validator plugin

I use jquery validator plugin to validate my form. 我使用jquery验证器插件来验证我的表单。 In form exists currency field, which needs the value, that appropriate to this regexp ^\\d{1,9}(\\.\\d{1,2})?$ . 在表单中存在currency字段,它需要适合此正则表达式的值^\\d{1,9}(\\.\\d{1,2})?$ Below is my js code snippet. 下面是我的js代码片段。

$("#myForm").validate({
        ignore:[],
        rules:{
            currency:{
                required: true
            }
            .....
        }
}

Question: How can I set above mentioned regexp to currency field with jquery ? 问题:如何使用jquery将上述regexp设置为currency字段?

You could define a custom rule: 您可以定义自定义规则:

$.validator.addMethod('currency', function(value, element, regexp) {
    var re = /^\d{1,9}(\.\d{1,2})?$/;
    return this.optional(element) || re.test(value);
}, '');

and then: 接着:

$('#myForm').validate({
    ignore:[],
    rules: {
        currency: {
            currency: true
        }
        .....
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM