1

メール確認フォームを 3 つの部分でコーディングしています。

パート 1 - 許可された文字のリストに対して単一の文字をチェックし、true/false を返します。

パート 2 - 連続する各文字に対して前の関数を呼び出すループを使用して、「@」の前後の部分として文字列をチェックします。

パート 3 - 完全なメールをチェックして、「@」が 1 つだけ含まれていること、「@」の前後の部分文字列が両方ともパート 2 を満たしていること、「@」に続く部分文字列にピリオドが 1 つしかないことを確認します。

パート 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 では、変数はさまざまなタイプを持つことができます: boolean、integer、float、string など。同じタイプの変数しか比較できません (にんじんとじゃがいもを混ぜないでください!)。たとえば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 に答える