0

im new to jquery validator, im trying to use regex to check if numbers, capital and special characters are included. I have the following html

<div class="wrapper wrapper-content animated fadeInRight">
<div class="row white-bg spacepad">
    <div id="keypass">
        <form method="POST" class="form-horizontal" id="keyform" name="keypasser">
            <div class="form-group"><label class="col-sm-2 control-label">Decryption Key For EPins</label>
                <div class="col-sm-10"><input type="text" required="required" class="form-control" name="keypassx" /></div>
            </div>
            <div class="form-group"><label class="col-sm-2 control-label">Confirm Decryption Key</label>
                <div class="col-sm-10"><input type="text" required="required" class="form-control" name="keypass2" /></div>
            </div>
            <input type="submit" class="btn btn-primary btn-rounded btn-block" value="Submit" />
        </form>
    </div>
</div>

and the following jquery, when i remove the addmethod it works, please what am i doing wrong

<script type="text/javascript">
$(function() {
// Initialize form validation on the registration form.
// It has the name attribute "registration"
$("form[name='keypasser']").validate({
    // Specify validation rules
    rules: {
        keypassx: {
            required: true,
            minlength: 8,
            passchecker: true,
        },
        keypass2: {
            required: true,
            equalTo: "#keypassx",
        }
    },
    // Specify validation error messages
    messages: {
        keypassx: {
            required: "Epins password required",
            minlength: "Minimum length allowed is 8 characters",
            passchecker: "Password should contain numbers, special characters and one uppercase",
        },
        keypass2: {
            required: "Confirmation password requied",
            equalTo: "Password does not match Epins Password",
        }
    },

    $.validator.addMethod("passchecker",
        function(value, element) {
             return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value);
    });

    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
        form.submit();
    }
});

});
</script>

kindly help out

soloNG
  • 21
  • 3
  • Possible duplicate of [jQuery Validate plugin - password check - minimum requirements - Regex](http://stackoverflow.com/questions/18746234/jquery-validate-plugin-password-check-minimum-requirements-regex) – soloNG Apr 23 '17 at 11:23

2 Answers2

0

Please try to use regular expression as

var patt = new RegExp("e"); var res = patt.test(str);

You directly use the regular expression. Please create object and try it.

0

The addMethod function accepts three params check the docs:

$.validator.addMethod("passchecker",
        function(value, element) {
             if(/^[A-Za-z0-9\d=!\-@._*]*$/.test(value)) {
                return true;
             } else {
                return false;
             }
    }, "Your validation message goes here");
Amr Aly
  • 3,871
  • 2
  • 16
  • 33