0

I have javascript validation code, now I want to prevent . ; , ` :

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.

4

2 回答 2

1

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
},
于 2012-12-06T07:31:37.490 回答
1

You need a regex like this ?

jQuery.validator.addMethod("noSpecials", function(value, element) {
    return !value.match(/[.;,]/g);
});
于 2012-12-06T07:33:22.950 回答