使用Jquery验证添加自定义错误消息

时间:2016-09-09 22:55:42

标签: javascript jquery forms validation jquery-ui

我正在尝试为我的字段验证添加自定义错误消息,而不是使用默认消息,但在尝试了许多不同的选项后它无法正常工作。以下是我的代码段。请帮忙!

$('#myform').validate({ // initialize the plugin
        submitHandler: function (form) {
            console.log("Submitted!");
       },
        rules: {
            accountNumber: {
                required: true,
                minlength: 8,
                maxlength: 8,
                min: 11111111,
                digits: true

                },
            companyNumber: {
                required: true,
                digits: true

            }
        },

       messages: {
           min: 'Account number cannot be 00000000',
           max: 'Account number cannot be 00000000',
           digits: 'The account number must be a number'
       }

    });

});

1 个答案:

答案 0 :(得分:0)

试试这个:

    $('#myform').validate({ // initialize the plugin
    submitHandler: function (form) {
        console.log("Submitted!");
   },
    rules: {
        accountNumber: {
            required: true,
            minlength: 8,
            maxlength: 8,
            min: 11111111,
            digits: true

            },
        companyNumber: {
            required: true,
            digits: true

        }
    },

   messages: {
   accountNumber: {
       min: 'Account number cannot be 00000000',
       max: 'Account number cannot be 00000000',
       digits: 'The account number must be a number'
   },
        companyNumber: {
        digits: 'The account number must be a number'
   }      
});});