函数内的循环函数?

时间:2012-09-16 11:35:23

标签: function for-loop counter

我正在分三部分编写电子邮件验证表。

第1部分 - 针对允许的字符列表检查单个字符并返回true / false。

第2部分 - 使用循环调用前一个函数到每个连续的字符,检查一个字符串作为'@'之前或之后的部分。

第3部分 - 检查一个完整的电子邮件,其中只包含一个'@','@'之前和之后的子字符串都满足第2部分,而'@'之后的子字符串只有一个句号。

我已经完成了第1部分,但第2部分的循环不正确,并且对于除空白表单之外的所有输入值都返回true。这是代码 -

        function isValidEmailPart(part)
        {   var emailPartInput = document.getElementById("isValidPartArg").value;
    var emailPartLength = alert(emailPartInput.length);
    {
    if (emailPartInput.length == "")
      {
      return (false)
      }
      else
      {
    NUMBER_OF_CHARACTERS = alert((emailPartInput.length) - 1);
    var i = 0;
      {for(var i=0; i<NUMBER_OF_CHARACTERS; i++)
        {
        function isValidEmailChar()
          { var validChars = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,_,-,.,';
            var emailPartInput = document.getElementById("isValidPartArg").value;
            var charInput = emailPartInput.charAt(i);
            var inputVar = validChars.indexOf(charInput);
            if (inputVar < 0)
            {
            return (false)
            }
          }
        }
        return (true);
      }
      }
    }
     } 

我知道它必须是简单的东西,没有错误返回我不知道我做错了什么。

1 个答案:

答案 0 :(得分:1)

请仔细考虑以下事项:

  • 单独定义函数:您可以从另一个函数调用函数但不在函数内定义函数

  • 确保您的代码正常,请注意您的代码语法:我发现了其他{。通常,您的代码编辑器会突出显示代码语法错误。

  • 注意代码的缩进:有一个好的缩进可以帮助您更清楚地查看代码并帮助您找到潜在的代码错误。

  • 在javascript中查看不同类型的变量:,变量可以有不同的类型:布尔值,整数,浮点数,字符串等。您只能比较相同类型的变量(不要混合胡萝卜和土豆!)因此,您无法将emailPartInput与空字符串""进行比较。

  • 在阅读下面的代码之前,您应该尝试搜索代码的错误,以及必须修改哪些内容才能使其正常工作。

  • 仔细检查我在后面的代码中写的评论(我花了很多时间写它们!)


javascript功能:

// This functions verifies if a char 'my_char' is valid
function isValidEmailChar(my_char)
{ 
    // 'my_char' is a i-th character of 'emailPartInput'
    var output = false;

    // 'validChars' is the array containing all the valid characters
    var validChars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                      'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                      '0','1','2','3','4','5','6','7','8','9','_','-','.'];

    // We want to check if 'my_char' is in the array 'validChar'
    // So, for each character in the array 'validChar', we check that there's at least
    //  1 character in it which is equal to 'my_char'
    for(var i=0; i<validChars.length; i++)
    {
        // 'output' is the result that the function 'isValidEmailChar' will return
        // It is initially set to "false"
        // The line below means: we store in 'output' 
        //   the result of " output OR ['my_char' EQUALS the i-th character in the array 'validChars'] ".
        //   Which means that, in the end, 'output' will be "true" if there's at least one i-th character 
        //      in the  array 'validChars' where 'my_char' EQUALS the i-th character in the array  'validChars'.
        output = (output || (my_char == validChars[i]));
    }

    // We return the output
    // Note: It is better to define 1 'return' and not several
    return output;
}

// This function verifies if a part of Email is valid
function isValidEmailPart(emailPartInput)
{   
    // 'emailPartInput' is the part of email

    // 'output' is your function's result to be returned
    var output = false;

    alert("INPUT = "+emailPartInput);

    var nb_of_characters = emailPartInput.length;       
    alert("number of characters = "+nb_of_characters);

    if (nb_of_characters != 0)
    {
        output = true;
        var i = 0;

        while(output && i<nb_of_characters)
        {
            // 'is_character_valid' is a boolean value which is set to:
            //     - true: if the i-th character of 'emailPartInput' is valid
            //     - false: if not valid
            var is_character_valid = isValidEmailChar(emailPartInput.charAt(i));

            // The line below means that we store in the variable 'ouput' the result of
            //    'output' AND 'is_character_valid', which means that:
            //        if there's at least one 'is_character_valid' set to false 
            //        (= one i-th character of 'emailPartInput' is not valid)
            //        'output' will then be equals to false
            output = output && is_character_valid;

            i++;

            // We remark that if 'output' is false, we quit the 'while' loop
            // because finding one invalid character means that 'emailPartInput' is invalid
            // so, we do not need to check the other characters of 'emailPartInput'
        }
    } 
    else 
    {
        alert("No emailPartInput has been input"); 
    }

    // We return the output
    return output;
}

以下是一个可以测试功能的工作示例:

<HTML>
    <HEAD>
        <SCRIPT language="javascript">

        // This functions verifies if a char 'my_char' is valid
        function isValidEmailChar(my_char)
        { 
            // 'my_char' is a i-th character of 'emailPartInput'
            var output = false;

            // 'validChars' is the array containing all the valid characters
            var validChars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                      'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                      '0','1','2','3','4','5','6','7','8','9','_','-','.'];

            // We want to check if 'my_char' is in the array 'validChar'
            // So, for each character in the array 'validChar', we check that there's at least
            //  1 character in it which is equal to 'my_char'
            for(var i=0; i<validChars.length; i++)
            {
                // 'output' is the result that the function 'isValidEmailChar' will return
                // It is initially set to "false"
                // The line below means: we store in 'output' 
                //   the result of " output OR ['my_char' EQUALS the i-th character in the array 'validChars'] ".
                //   Which means that, in the end, 'output' will be "true" if there's at least one i-th character 
                //      in the  array 'validChars' where 'my_char' EQUALS the i-th character in the array  'validChars'.
                output = (output || (my_char == validChars[i]));
            }

            // We return the output
            // Note: It is better to define 1 'return' and not several
            return output;
        }

        // This function verifies if a part of Email is valid
        function isValidEmailPart(emailPartInput)
        {   
            // 'emailPartInput' is the part of email

            // 'output' is your function's result to be returned
            var output = false;

            alert("INPUT = "+emailPartInput);

            var nb_of_characters = emailPartInput.length;       
            alert("number of characters = "+nb_of_characters);

            if (nb_of_characters != 0)
            {
                output = true;
                var i = 0;

                while(output && i<nb_of_characters)
                {
                    // 'is_character_valid' is a boolean value which is set to:
                    //     - true: if the i-th character of 'emailPartInput' is valid
                    //     - false: if not valid
                    var is_character_valid = isValidEmailChar(emailPartInput.charAt(i));

                    // The line below means that we store in the variable 'ouput' the result of
                    //    'output' AND 'is_character_valid', which means that:
                    //        if there's at least one 'is_character_valid' set to false 
                    //        (= one i-th character of 'emailPartInput' is not valid)
                    //        'output' will then be equals to false
                    output = output && is_character_valid;

                    i++;

                    // We remark that if 'output' is false, we quit the 'while' loop
                    // because finding one invalid character means that 'emailPartInput' is invalid
                    // so, we do not need to check the other characters of 'emailPartInput'
                }
            } 
            else 
            {
                alert("No emailPartInput has been input"); 
            }

            // We return the output
            return output;
        }

        function test() {
            var my_input = document.getElementById("my_input").value;

            var result = isValidEmailPart(my_input);

            if(result) 
                alert("The part of email is valid");
            else
                alert("The part of email is NOT valid");
        }

        </SCRIPT>
    </HEAD>
    <BODY>
        Enter you Email part here:
        <INPUT type="text" id="my_input" value="" />
        <button onclick="javascript:test();">Check the Email part!</button>   
    </BODY>
</HTML>

NB:最重要的是确保您了解自己在代码中写的内容以及出了什么问题。

我想你知道只是复制工作对你没有好处。

如果您阅读我的代码,我希望您花时间了解它并仔细阅读评论(我花了很多时间来编写它们!:S)

您可以查看免费的在线教程来学习javascript! :)

希望这会有所帮助。如果您有任何疑问,请不要犹豫,我很乐意提供帮助。