简体   繁体   English

Parsley.js中的自定义正则表达式验证器

[英]Custom Regex Validator in Parsley.js

Issue 问题

I am having a problem creating a custom validator for the Parsley.js plugin. 我在为Parsley.js插件创建自定义验证程序时遇到问题。 What I'm trying to do is test a value vs a regex. 我想做的是测试值与正则表达式。 Specifically, I'm trying to validate that a password value includes at least one uppercase and one lowercase letter. 具体来说,我正在尝试验证密码值至少包含一个大写字母和一个小写字母。 The validation code throws an error, but does not return true when the condition has been satisfied. 验证代码将引发错误,但在满足条件时不会返回true。

HTML 的HTML

<form name="Form" id="signupform" method="post" action="#" data-parsley-validate data-parsley-excluded="input[type=button], input[type=submit], input[type=reset], input[type=hidden], [disabled], :hidden">
      <label for="password1">New Password <span class="req">*</span></label>
      <input name="password" id="password1" type="password" class="password" data-parsley-minlength="1" data-parsley-errors-container=".errorspannewpassinput" data-parsley-required-message="Please enter your new password." data-parsley-mixedcase="^(?=.*[a-z])(?=.*[A-Z]).*$" data-parsley-required/>
      <span class="errorspannewpassinput"></span>
      <label for="confirm_password1">Confirm Password <span class="req">*   </span></label>
      <input name="Password_2" id="password2" type="password" class="password" data-parsley-minlength="1" data-parsley-errors-container=".errorspanconfirmnewpassinput" data-parsley-required-message="Please re-enter your new password." data-parsley-equalto="#password1" data-parsley-mixedcase="^(?=.*[a-z])(?=.*[A-Z]).*$" data-parsley-required />
      <span class="errorspanconfirmnewpassinput"></span>
      <input type="submit" name="submitinfo" id="submitsignup" data-theme="b" style="width:100%;" alt="Sign Up Now" value="Submit Account Request" />
</form> 

jQuery jQuery的

  $(function() {
    // Set variables
    var pmixedCase = 'Y';
    var mixedCase = $('.mixedcase');
    var passwordField = $('#password1, #password2');
    var passwordFieldErrors = $('.errorspannewpassinput, .errorspanconfirmnewpassinput');
    // Assemble list of visible password requirements
    // Mixed Case
    if (pmixedCase === 'Y') {
        mixedCase.show();
    } else {
        mixedCase.hide();
    }
    // Custom Validators
    window.Parsley.addValidator('mixedcase', {
        requirementType: 'regexp',
        validateString: function(value, requirement) {
            return requirement.test(value);
        },
        messages: {
            en: 'Your password must contain at least (1) lowercase and (1) uppercase letter.'
        }
    });
}); 

I've included a JSFiddle below. 我在下面包括了一个JSFiddle。

Fiddle 小提琴

The issue you're facing is that your JsFiddle is using Parsley 2.1.2 whereas the documentation is already updated to Parsley 2.2.0. 您面临的问题是您的JsFiddle使用的是Parsley 2.1.2,而文档已更新为Parsley 2.2.0。

If you look at the Javascript console you'll see an error: 如果您查看Javascript控制台,将会看到错误:

Uncaught TypeError: window.Parsley.addValidator is not a function 未捕获的TypeError:window.Parsley.addValidator不是函数

Which means that the version you're using is not yet updated (the previous versions used window.ParsleyValidator.addValidator ). 这意味着您正在使用的版本尚未更新(以前的版本使用window.ParsleyValidator.addValidator )。 So, if you simply update Parsley.js to the correct version your code will work. 因此,如果您只是将Parsley.js更新到正确的版本,则代码将起作用。 See this fiddle . 看到这个小提琴


However, there's a simpler way to accomplish what you need (that is, without a custom validator). 但是,有一种更简单的方法来完成您需要的操作(即没有自定义验证器)。 You can use the built-in Pattern ( data-parsley-pattern ). 您可以使用内置的Pattern( data-parsley-pattern )。 For example: 例如:

<form name="Form" id="signupform" method="post" data-parsley-validate data-parsley-excluded="input[type=button], input[type=submit], input[type=reset], input[type=hidden], [disabled], :hidden">
    <label for="password1">New Password <span class="req">*</span></label>
    <input name="password" id="password1" type="password" class="password" data-parsley-minlength="1" data-parsley-errors-container=".errorspannewpassinput" data-parsley-required-message="Please enter your new password." data-parsley-pattern="(?=.*[a-z])(?=.*[A-Z]).*" data-parsley-pattern-message="Your password must contain at least (1) lowercase and (1) uppercase letter." data-parsley-required />
    <span class="errorspannewpassinput"></span>
    <label for="confirm_password1">Confirm Password <span class="req">*</span></label>
    <input name="Password_2" id="password2" type="password" class="password" data-parsley-minlength="1" data-parsley-errors-container=".errorspanconfirmnewpassinput" data-parsley-required-message="Please re-enter your new password." data-parsley-equalto="#password1" data-parsley-pattern="(?=.*[a-z])(?=.*[A-Z]).*" data-parsley-required data-parsley-pattern-message="Your password must contain at least (1) lowercase and (1) uppercase letter."/>
    <span class="errorspanconfirmnewpassinput"></span>
    <input type="submit" name="submitinfo" id="submitsignup" data-theme="b" style="width:100%;" alt="Sign Up Now" value="Submit Account Request" />
</form>

What I've changed: 我改变了什么:

  1. Replaced data-parsley-mixedcase="^(?=.*[az])(?=.*[AZ]).*$" for data-parsley-pattern="(?=.*[az])(?=.*[AZ]).*" on both inputs. 替换data-parsley-mixedcase="^(?=.*[az])(?=.*[AZ]).*$"data-parsley-pattern="(?=.*[az])(?=.*[AZ]).*"在两个输入上。

    As per Marc-André Lafortune 's comment I removed the ^ at the start and the $ at the end, because the patterns are now anchored. 根据Marc-AndréLafortune的评论,我删除了开头的^和结尾的$ ,因为这些模式现在已锚定。

  2. Added data-parsley-pattern-message="Your password must contain at least (1) lowercase and (1) uppercase letter." 添加了data-parsley-pattern-message="Your password must contain at least (1) lowercase and (1) uppercase letter." on both inputs. 在两个输入上。

JsFiddle available here . JsFiddle 在这里可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM