1

我正在编写 3 个部分的电子邮件验证表。

第 1 部分 - 根据允许的字符列表检查单个字符并返回真/假。

第 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);
      }
      }
    }
     } 

我知道这一定很简单,没有返回错误我不知道我做错了什么。

4

1 回答 1

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>

注意:最重要的是确保您了解您在代码中编写的内容以及错误所在。

我想你知道仅仅复制一个作品对你没有好处。

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

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

希望这可以帮助。如果您有任何问题,请随时提问,我很乐意为您提供帮助。

于 2012-09-16T13:12:32.480 回答