如何在jQuery中检测特殊字符的数量

时间:2019-03-18 10:54:48

标签: jquery passwords jquery-validate special-characters

输入密码时,我需要检查其长度以及是否至少有3类字符(对于magento)

我正在使用jqueryValidate作为长度,我也需要使用它作为密码。

用特殊字符表示小写,大写,数字和特殊字符。

我该怎么做?

这就是现在所拥有的:

    $('#form').validate({
        rules : {
            "password" : {
                minlength : 8,
                required : true
            },
            "email" :{
                required: true,
                email: true
            }

        }
    });

1 个答案:

答案 0 :(得分:0)

<script>
  jQuery.validator.addMethod('specialChars', function(value, element, num) {
    var specialChars = value.replace(/[A-Za-z0-9 ]/g, '');
    return this.optional(element) || specialChars.length > num;
  }, 'At least {0} special chars are required');

  $("form").validate({
    rules: {
      password: {
        specialChars: 3,
        required: true,
      }
    }
  });
</script>

jQuery验证提供了有用的API,您可以使用它来创建新的自定义规则。希望它很有用。

相关问题