The Bijis
The Bijis

Reputation: 19

Javascript prevent dot comma etc

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.

Upvotes: 0

Views: 1339

Answers (2)

showi
showi

Reputation: 486

You need a regex like this ?

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

Upvotes: 1

Cerbrus
Cerbrus

Reputation: 72907

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

Upvotes: 1

Related Questions