简体   繁体   中英

Custom Regex Validator in Parsley.js

Issue

I am having a problem creating a custom validator for the Parsley.js plugin. 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.

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

  $(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.

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.

If you look at the Javascript console you'll see an error:

Uncaught TypeError: window.Parsley.addValidator is not a function

Which means that the version you're using is not yet updated (the previous versions used window.ParsleyValidator.addValidator ). So, if you simply update Parsley.js to the correct version your code will work. 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 ). 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.

    As per Marc-André Lafortune 's comment I removed the ^ at the start and the $ at the end, because the patterns are now anchored.

  2. Added data-parsley-pattern-message="Your password must contain at least (1) lowercase and (1) uppercase letter." on both inputs.

JsFiddle available here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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