0

我创建了这个函数,它检查多个文本框是否具有 onsubmit 值。基本上它是一个 javascript 表单验证器。当有空文本框时,表单不应提交并提醒用户有必填字段。现在,这对我来说非常有用,但是,当已经有值并且提交了表单时,即使应该提交,它仍然没有提交。我创建了一个 if 语句来检查 errorString 是空还是 null,如果是,则表单应该提交,但会发生什么是它用空字符串提醒用户。我认为代码仍在 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'条件,结果为真(因为errorString是“”而不是空),然后进入代码块。

于 2013-09-25T12:46:29.557 回答
0

错误的行是每个人都指出的:

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

我想指出您不需要检查,errorString!=null因为您已经在代码开头将变量创建为空字符串:

var errorString = ""; //created errorString as empty string

因此它实际上永远不会是null,因为您从未在任何地方将其设置为 null (也就是说,您从未将其设置为errorString = null)。

如果没有错误,它将始终是一个空字符串。所以这就足够了:

        if (errorString!="") //if errorString is different than what we created at `var`
        {
            alert(errorString);
            return false;
        }
        else
        {
            return true;
        }
于 2013-09-25T12:50:51.417 回答
0

errorString 永远不能为空,因为您已使用“”对其进行了初始化,因此您可以删除条件“errorString != null”。

它应该成为

        if (errorString!="")
        {
            alert(errorString);
            return false;
        }
        else
        {
            return true;
        }
于 2013-09-25T12:50:54.427 回答
0

改变

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

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

您有两个相互抵消的“非”语句(一个必须始终为假)。将其更改为:

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

尝试不同的逻辑。您可以使用波纹管代码检查所有四 (4) 个条件以进行验证,例如非空、非空白、非未定义和非零仅在 javascript 和 jquery 中使用此代码 (!(!(variable)))。

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 回答