Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

I have this:

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

    rules: {
        someName: {
            required: true,
            minlength: 5,
            maxlength: 8,
        },
        somePassword: {
            required: true,
                            minlength: 5,
            maxlength: 8,
        },
        someConfirmPassword: {
            required: true,
            equalTo: "#somePassword"
        },
    },
    messages: {
        someName:  {
            required: "Please create your username",
        },
        somePassword: {
            required: "Please create a password",
        },
        someConfirmPassword: {
            required: "Please re-enter your password",
            equalTo:  "Passwords must match"
        },
    }
});

});

And I would like to check for specific characters that are allowed in the DB for passwords when the user enters their data here:

    <label for="someName">Username</label>
    <input type="text" id="someName" name="someName" placeholder="Create a Username"/>

    <label for="somePassword">Password</label>
    <input type="password" id="somePassword" name="somePassword" placeholder="Create a Password"/>

    <label for="someConfirmPassword">Confirm Password </label>
    <input type="password" id="someConfirmPassword" name="someConfirmPassword" placeholder="Verify your Password"/>

What's the best way to add a regex check to a required field such as a password or username using jquery.validate plugin from bassisstance: http://docs.jquery.com/Plugins/Validation

Basically, I need to make sure that the password and usernames they create only have 0-9, letters, and a couple of special characters.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
747 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...