0

送信時に複数のテキストボックスに値があるかどうかをチェックするこの関数を作成しました。基本的には、JavaScript フォーム バリデーターです。空のテキスト ボックスがある場合、フォームは送信されず、必須フィールドがあることをユーザーに警告する必要があります。今、これは私にとっては完全に機能しますが、すでに値があり、フォームが送信された場合でも、送信する必要がありますが送信されません。errorString が空か null かどうかを確認する if ステートメントを作成しました。空である場合はフォームを送信する必要がありますが、空白の文字列でユーザーに警告します。コードはまだ if(errorString!=null || errorString=="") ステートメント内にあると思いますが、そうすべきではありません。

前もって感謝します

以下の私のコードを見てください:

            function validateForm()
        {
            var txtTitle = document.forms["actionStepsForm"]["txtTitle"].value;
    var txtRequestor = document.forms["actionStepsForm"]["txtRequestor"].value;
            var txtReprocessingRequest = document.forms["actionStepsForm"]["txtReprocessingRequest"].value;

            document.getElementById('rowCount').value = counter-1;

    var errorString = "";

            if (txtTitle==null || txtTitle=="")
            {
                errorString += "Title field is required. \n";
            }
            if (txtRequestor==null || txtRequestor=="")
            {
                errorString += "Requestor field is required. \n";
            }
            if (txtReprocessingRequest==null || txtReprocessingRequest=="")
            {
                errorString += "Reprocessing request FR is required. \n";
            }


            if (errorString!=null || errorString!="")
            {
                alert(errorString);
                return false;
            }
            else
            {
                return true;
            }

        }

//implementation if HTML form
<form name="actionStepsForm" id="actionStepsForm" action="add_reprocessing.php?action=add" method="POST" onsubmit="return validateForm()">
4

6 に答える 6

0

正しい状態は

if (errorString != null && errorString != "")

それ以外の場合、エンジンは 'errorString != null' 条件を評価し、true と評価され (errorString は "" で null ではないため)、コード ブロックに入ります。

于 2013-09-25T12:46:29.557 に答える
0

変化する

if (errorString!=null || errorString!="")

if (errorString!=null && errorString!="")
于 2013-09-25T12:46:31.000 に答える
0

互いに相殺する 2 つの 'not' ステートメントがあります (一方は常に false でなければなりません)。次のように変更します。

    if (errorString!=null && errorString!="")
    {
        alert(errorString);
        return false;
    }
    else
    {
        return true;
    }
于 2013-09-25T12:46:51.263 に答える
0

別のロジックで試してください。次のコードを使用して、null でない、空白でない、未定義でない、ゼロでないなどの検証の 4 つの条件すべてをチェックできます。javascript および jquery でこのコード (!(!(変数))) のみを使用します。

function myFunction() {
        var errorString;  //The Values can be like as null,blank,undefined,zero you can test

         if(!(!(errorString)))  //this condition is important
         {
         alert("errorString"+errorString);
         } 
         else 
         {
            alert("errorStringis "+errorString);
        }

        }
于 2018-01-17T13:00:06.933 に答える