Javascript防止点逗号等

时间:2012-12-06 07:16:58

标签: javascript jquery jquery-validate validation

我有javascript验证码,现在我想阻止。 ; ,`:

到目前为止我的代码:

jQuery.validator.addMethod("noSpace", function(value, element) {
    return !value.match(/\s/g);
});

$().ready(function() {
    $("#signup_form").validate( {
        rules: {
            full_name: "required",
            username: {
                required: true,
                minlength: 2,
                noSpace: true,
            },
            password: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
        },
        messages: {
            full_name: "<div class='spacing'>Please enter your full name</div>",
            username:
            {
                required: "<div class='spacing'>Please enter an username</div>",
                minlength: "<div class='spacing'>Your username must consist of at least 2 characters</div>",
                noSpace: "<div class='spacing'>No space allowed</div>",
            },
            password:
            {
                required: "<div class='spacing'>Please provide a password</div>",
                minlength: "<div class='spacing'>Your password must be at least 5 characters long</div>"
            },
            email: "<div class='spacing'>Please enter a valid email address</div>"
        }
    });
});

我想在用户名规则中阻止。因此,如果我输入. , ;等,它将显示不允许的消息。

2 个答案:

答案 0 :(得分:1)

未经测试,但这应该有效:

jQuery.validator.addMethod("userNameRule", function(string, element) { 
    return !string.match(/[.;,]/g);
}, "Your username contains invalid characters!");

username: {
    required: true,
    minlength: 2,
    noSpace: true,
    userNameRule: true
},

答案 1 :(得分:1)

你需要这样的正则表达式吗?

jQuery.validator.addMethod("noSpecials", function(value, element) {
    return !value.match(/[.;,]/g);
});