How can we specify rules for jquery validation plugin by class?

前端 未结 9 1430
一生所求
一生所求 2020-11-27 11:19

The jQuery Validation plugin works great and is very easy to use:

$(\".selector\").validate({
})

Just by setting css classes like \"require

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 12:07

    jQuery.validator.addClassRules(); will attach the validation to class, but there is no option for messages, it will use the general error messages.

    If you want that to work then, you should refactor the rules like this

    $.validator.addMethod(
         "newEmail", //name of a virtual validator
         $.validator.methods.email, //use the actual email validator
         "Random message of email"
    );
    
    //Now you can use the addClassRules and give a custom error message as well.
    $.validator.addClassRules(
       "email", //your class name
       { newEmail: true }
     );
    

提交回复
热议问题