开发者

How to add alphanumeric check to jQuery validation plugin?

This exact question was asked here:

using the jquery validation plugin, how can I add a regex validation on a textbox?

But unfortunately the solution posted isn't working for me. I am using the very popular jquery validation plugin, specifically this version:

http://jquery.bassistance.de/validate/jquery.validate.js

I tried adding the custom alphanumeric function (from the above stack overflow question) like so:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>

<script type="text/javascript">
$(function() {

    $.validator.addMethod("loginRegex", functi开发者_Python百科on(value, element) {
        return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
    }, "Username must contain only letters, numbers, or dashes.");

    $("#myForm").validate({
        rules: {
            "login": {
                required: true,
                loginRegex: true,
            }
        },
        messages: {
            "login": {
                required: "You must enter a login name",
                loginRegex: "Login format not valid"
            }
        }
    });

});
</script>

<form method="post" id="myForm">
    <input type="text" name="login" />  
    <input type="submit" value="Go" />  
</form>

If you run this code you can see that it only validates for some type of input, it doesn't validate the input through the custom alphanumeric function. How can this be made to work?


Could it be that you have an extra comma after your login rule? IE will have trouble with this:

rules: {
    "login": {
        required: true,
        loginRegex: true,
    }
},

should be:

rules: {
    "login": {
        required: true,
        loginRegex: true
    }
},
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜