简体   繁体   中英

alphanumeric validation javascript without regex 2

if i run, validation just can be work only on symbol "/", if I input the other symbol except / didnt working. I'm not use regex.

if(nama!==""){
    var i;
    var list = new Array ("/","!", "@", "#","$","%","%","^","&","*",
                          "(",")","_","+","=","-","`","~",";","<",
                          ">",".","?","[","]","{","}",",");

    var llength = list.length;
    for(i=0; i<llength; i++)
    {
        if(nama.match(list[i]))
        {
            alert("Full Name must not contain any number and symbol");
            return false;
        }
        else
        {
            return true;
        }
    }
}

There seem to be several problems here. One is that you are calling return true as soon as you reach a valid character. That means that you'll never check anything else if the first letter is valid.

Another problem is that you are trying to check for invalid characters, but how can you know you've checked them all?

A better approach to the whole problem might be to only a list of valid letters; The changes to your original could might be something like the following:

var list = new Array ("a", "A", "b", "B", ... [etc] );

for(i=0; i<llength; i++)
{
    if(!nama.match(list[i]))
    {
        alert("Full Name can only contain letters a-z!");
        return false;
    }
}

This should only quit the loop (and the containing function) when an invalid character is encountered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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