简体   繁体   English

Javascript防止点逗号等

[英]Javascript prevent dot comma etc

I have javascript validation code, now I want to prevent . 我有javascript验证码,现在我想防止 ; ; , ` : ,`:

this my code so far : 这个我的代码到目前为止:

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>"
        }
    });
});

I want to prevent in username rules. 我想在用户名规则中阻止。 So if I input . , ; 所以,如果我输入. , ; . , ; etc, it will show message that it can't allowed. 等,它会显示不允许的消息。

Not tested, but this should work: 没有测试,但这应该工作:

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

You need a regex like this ? 你需要像这样的正则表达式吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM