最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'Check for Letter' Validation Not Working - Stack Overflow

programmeradmin9浏览0评论

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 pleting 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.

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 pleting 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.

Share Improve this question edited Mar 6, 2011 at 18:57 Buns Glazing asked Mar 6, 2011 at 17:31 Buns GlazingBuns Glazing 1412 silver badges12 bronze badges 6
  • 1 OMG, what's going on here? 1) Getting a single character with substr 2) isNaN 3) if(true) return true else return false is unnecessary 4) log after return statement. This can be done a lot better... – Harmen Commented Mar 6, 2011 at 17:34
  • @Harmen > I'm sure it can, but since I'm still in my first Javascript class of my college program, 'substr' is what the prof gave us to work with. Also, I thought I'd taken out the 'return false' statements. :P – Buns Glazing Commented Mar 6, 2011 at 17:36
  • @GnomeSlice: textField.charAt(i) returns a letter at that position; 'return hasLetter' should be better, no condition statements ;) ... – Maxym Commented Mar 6, 2011 at 17:38
  • @GnomeSlice: do you check only if textField has at least one letter? – Maxym Commented Mar 6, 2011 at 17:39
  • What browser are you using for debugging this? – Gabriel Commented Mar 6, 2011 at 17:40
 |  Show 1 more ment

3 Answers 3

Reset to default 4

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 remend 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)

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";
}

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;
    }
发布评论

评论列表(0)

  1. 暂无评论