Help Leecher
Help Leecher

Reputation: 119

How to create jQuery Validator method for username constraints?

So I currently have this method for enforcing strong passwords:

$.validator.addMethod('goodPassword', function(value, element){
    return this.optional(element) ||
        value.length >= 6 &&
        /\d/.test(value) &&
        /[a-z]/i.test(value);
}, 'Your password sucks boy, you need at least 6 char and both number and char in that motherf.')

I want to create something similar for my usernames to limit them only to letters, numbers, dashes, underscores and periods. I can't seem to figure out how to go about it. I read about regex a little but still couldn't figure out how exactly I should go about my code.

This is what I currently have:

$.validator.addMethod('goodUsername', function(value, element)){
    return this.optional(element) ||
        value.length >= 4 // && or ||, not really sure what to put here
        //something to check if entered data contains anything other than letters, numbers, dashes, underscores and periods
}, 'What kind of fkin username is that? You can only use letters, numbers, dashes, underscores and periods.')

Can someone show me the way please?

Upvotes: 0

Views: 114

Answers (3)

andre mcgruder
andre mcgruder

Reputation: 1520

I got this Regex to work and based off the jQuery Validator documentaion.

/^[a-z|A-Z]|\d|_|-|\./m

Here is how it look in the code. jQuery validator addMethod

$.validator.addMethod('goodUsername', function(value, element)){
    return this.optional(element) || /^[a-z|A-Z]|\d|_|-|\./.test(value);
}, 'What kind of fkin username is that? You can only use letters, numbers, dashes, underscores and periods.');

I write use thie site to write my regular expressions regex 101

I ran it against these strings.

var str2 = 'uerhyeiufhsniufhsdJSHNAJDHJS09i304584305i4309SKA()*^&85$674&_-.dsf%#$fdfIIJ76..';
var str2 = 'dskfjdkaAHDNsfj34-2sds3432_-.*()$%#545@';

Upvotes: 1

buld0zzr
buld0zzr

Reputation: 962

Here's the code that uses Javascript regex functionality

$.validator.addMethod('goodUsername', function(value, element)){
    return this.optional(element) || value.match(/^[\w.-]{4,}$/m)
}, 'What kind of fkin username is that? You can only use letters, numbers, dashes, underscores and periods.')

As correctly noted by @rock321987, it'd still allow ...-... and other strange usernames.

Upvotes: 1

kiamoz
kiamoz

Reputation: 714

you can use this function and set your custom illegal charters :

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a username.\n";
        alert(error);
        return false;

    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow';
        error = "The username is the wrong length.\n";
        alert(error);
        return false;

    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow';
        error = "The username contains illegal characters.\n";
        alert(error);
        return false;

    } else {
        fld.style.background = 'White';
    }
    return true;
}

Upvotes: 1

Related Questions