add regex to jquery.validate

前端 未结 2 1158
情深已故
情深已故 2020-12-08 22:59

Need to add a regex check to my jquery.validate script.

I have this:

$().ready(function() {
  $(\"#myBlahForm\").validate({

    rules: {
        so         


        
相关标签:
2条回答
  • 2020-12-08 23:24

    well, you could add your custom validation method using addMethod, like

    $.validator.addMethod("regx", function(value, element, regexpr) {          
        return regexpr.test(value);
    }, "Please enter a valid pasword.");
    

    And ,

    ...
    $("#myBlahForm").validate({
    
        rules: {
            somePassword: {
                required: true ,
                //change regexp to suit your needs
                regx: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/,
                minlength: 5,
                maxlength: 8
            }
        }
    
    0 讨论(0)
  • 2020-12-08 23:44
    required: function(element){
        return /[a-zA-Z0-9]/.test($('#someConfirmPassword').val());
    }
    

    Above expression will return true if regexp passes, false if it doesn't. Change it to fit your needs.

    0 讨论(0)
提交回复
热议问题