Buns Glazing
Buns Glazing

Reputation: 141

'Check for Letter' Validation Not Working

I'm running some functions to verify that the user-entered text in a form contains at least one number and letter. It doesn't seem to be working. Since the two functions are nearly identical, I'll just post one of them:

function hasALetter(textField){
    //returns true or false based on whether or not the text field has at least one letter inside it    
    console.log("Checking for letters...");

    var hasLetter = false;

    for(var i=0, checkLength=textField.length; i<checkLength; i++){
        var letter = textField.substr(i,1);
        console.log("letter = " + letter);
        if(isNan(letter) == false){
            hasLetter = false;
        }
    }
    if(hasLetter == true){
        return true;
    }
}

The log ("letter = " + letter) never shows up in my console. I'm probably missing something silly, but it doesn't seem to be completing the function.

For reference, here is how I'm calling the functions:

if(pwd.value.length > 9){

            var pwdLetter = hasALetter(pwd);
            var pwdNumber = hasADigit(pwd);

            if(pwdLetter==true){
                if(pwdNumber==true){

Yes, I'm aware that it's very messy, but I'm still learning. I'm sure there's more advanced/cleaner ways of doing this validation, but for the purposes of my schooling, I'm doing it like this for now.

Upvotes: 2

Views: 3260

Answers (3)

Pointy
Pointy

Reputation: 413709

It's "isNaN", not "isNan" ... ... also, if it's not a number, that doesn't necessarily mean that it's not a letter; in fact the logic seems backwards, or upside-down.

If "isNaN" returns true, then it's not a number. Is it a letter? Well, you have to check. If "isNaN" returns false, then all you know is that it's one of the characters '0' through '9'.

The whole thing is much better done with a regex, but if this is homework it may be that that approach can't be used. You can check to see if a character is a letter with something like:

function isLetter(c) {
  c = c.toUpperCase();
  return c >= "A" && c <= "Z";
}

Upvotes: 4

iwalkbarefoot
iwalkbarefoot

Reputation: 955

I would make use of regular expressions, here are a couple of quick functions.

    function hasALetter(text)
    {
        var regex = new RegExp("[a-zA-Z]");
        if(text.match(regex))
            return true;
        else
            return false;
    }

    function hasANumber(text)
    {
        var regex = new RegExp("[0-9]");
        if(text.match(regex))
            return true;
        else
            return false;
    }

Upvotes: 0

Harmen
Harmen

Reputation: 22438

The easiest way to check if a string has a letter is the Regular Expression:

function hasLetter(str){
  // check for characters between a and z
  // i flag makes it case insensitive
  return /[a-z]/i.test(str);
}

If you want to loop through the string and also want to use the isNaN function, this would do:

function hasLetter(str){

  // loop through every character
  for(var i=0; i<str.length; i++){

    // check if the i-th character is not a number
    if(isNaN(str[i])){

      // if so, return true
      return true;
    }
  }

  // if the loop has finished and no letters have been found, return false
  return false;
}

But I would not recommend this method, because isNaN checks whether the first argument is a number or not. First of all, not all characters that aren't numbers are letters. Secondly, the argument you pass in is a string (str[i] returns a character of type string, even if it is a digit)

Upvotes: 4

Related Questions