开发者

jQuery Validate Plugin: check for alphanum values?

anyone ever worked with the jQuery Validate Plugin.

I have like the following function.

$("#registrat开发者_运维百科ionForm").validate({
        rules: {
            'user[login]': {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            'user[email]': {
                required: true,
                email: true
            },

I want to check the user[login] input for [0-9a-zA-Z-_] !

Any idea how to do so? Only alphanum -_ should be allowed.


Wow, sorry: forgot that regexp is a custom method I've added in my own code!

Try this:

$.validator.addMethod("regexp", function(value, element, pattern) {
    return this.optional(element) || pattern.test(value);
}, "Field contains invalid characters.");

And then:

$("#my-form").validate({
    rules: {
        "my-field": {
            required: true,
            regexp: /^[0-9a-zA-Z_]*$/
        }
    }
});

Take a look here.

Original (wrong) answer:

I'm pretty sure you just need to add this:

regexp: /^[0-9a-zA-Z-_]*$/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜