I want to check if nickname contains only numbers and letters. It cant contain anything else. My current code is not working because as soon as I type in atleast one legal lettrer, the entire string evaluates to true every time I check, no matter how many illegal characters it contains. What am I doing wrong here.

My code (Ignore the commented part):

$(function(){
    $("#nickname").keyup(function(event){
        var validate = new RegExp("[A-Za-z0-9]"); // All letters and digits, including "_"
        var nickname = $(this).val();
        if(!validate.test(nickname)){
            console.log("Did not validate");
            $("#message-area").html("Kasutajanimi koosneb lubamatutest sümbolitest. Kasutajanimi võib koosnema tähtedest, numbritest ja alakriipsudest ('_')");
        }
        /*
        $.ajax({
            type: "POST",
            url: "login.php",
            data: {"nickname" : nickname},
            dataType: "json",
            success: function(data){
                $("#message-area").html(data.message);

                if(data.available){
                    $("#choose-name").val('Sisene');
                }else{
                    $("#choose-name").val('Kontrolli');
                }
            }
        });*/
    });
});

I guess it's a question of getting the regexp correct, then knowing what to do with it.

Something like this maybe :

$("#nickname").keyup(function(event) {
    var validate = /^(_|[A-Za-z0-9])*$/;
    var nickname = $(this).val();
    var ok = validate.test(nickname);
    $("#message-area").html(ok ? 'ok' : 'not OK');
});
commented: Thanks for help, worked perfectly +2

About your regex:
You are checking only the first letter of your input string :).

[A-Za-z0-9] checks one characters
[A-Za-z0-9]+ checks 1 - infinity characters (until one is found that does not match), which means that at least one MUST match
[A-Za-z0-9]* checks 0 - infinity characters, which means no characters MUST match
[A-Za-z0-9]*? checks 0 - infinity characters (until one is found that does not match (that's what the questionmark does)), which also means that no characters MUST match

In the example that Airshow is giving:

/^(_|[A-Za-z0-9])*$/
What this regex means is: May be empty (because of the asterix (*)) or, if it is not empty, it must have only underscores and or letters and numbers.

^ defines the start of a string
$ defines the end of a string
(_|[A-Za-z0-9]) means either underscore or letters or numbers

What should work:
/^[a-z0-9]+$/i
What this regex means is: Must start with a number or letter, must contain at least one character, may contain only numbers and letters, is a case insensitive check (that's what the "i" means after the last backslash).

Minitauros, I deliberately used * rather than + in order that '' should be legal, which is more normal for 'keyup' validation. Typically you don't want the blank state of an input field to give an error message on keyup, neither initially nor on deletion of last remaining character.

'Submit' validation is different. In that case, we would be looking to exclude the blank state and all other illegal character combinations.

That is completely true ^^. I didn't mean to say that your code is wrong in any way, just wanted to explain the topic starter some stuff about regular expressions, so that he could pick the one that suits him best.

Cool.

Worked like a charm, thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.