0

jquery バリデータ プラグインを使用してフォームを検証します。この regexp に適切な値を必要とするフォームの existscurrencyフィールド^\d{1,9}(\.\d{1,2})?$。以下は私のjsコードスニペットです。

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

質問: 上記の正規表現をcurrencyフィールドに設定するにはどうすればよいjqueryですか?

4

1 に答える 1

3

カスタムルールを定義できます。

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

その後:

$('#myForm').validate({
    ignore:[],
    rules: {
        currency: {
            currency: true
        }
        .....
    }
}
于 2012-07-17T07:10:03.263 に答える