表单在formvalidation上提交false

时间:2018-09-02 20:35:49

标签: javascript

我很困惑,尽管我的formvalidation函数返回false,为什么我的表单仍然提交。以下是我的代码,我几乎无法弄清楚我做错了什么。请帮助大家

这是我的表单脚本

<form name="regForm" action= "/upload.php" method="post" enctype= "multipart/form-data" onsubmit="return validateForm();">
<p><span class="error" id="reportErr"></span></p>
<label>Do you have an Agent?</label>&nbsp;&nbsp;<input type="radio" name="agent" value="YES"><label>YES</label><input type="radio" name="agent" value="NO"><label>NO</label
<label>Are you under a contract</label><input type="radio" name="contract" value="YES"><label>YES</label><input type="radio" name="contract" value="NO"><label>NO</label>
<input type="submit" name="submit" value="Preview" <input type="reset"  value="Clear">

javascript函数如下:

function validateForm(){

var formvalid= true;//even when i set to be false it still submits form

//check for agent user selection


var agent = document.forms["regForm"]["agent"].value;


if (agent == "") {
    document.getElementbyId("reportErr").innerHTML = "Select if you have an agent or not";
    formvalid=false;
}else{

      if (agent =="YES"){
        //handle user input for agentname

         }
       }


      }
//check for contract user selection
var contract = document.forms["regForm"]["contract"].value;
if (contract==""){
   document.getElementbyId("reportErr").innerHTML = "Select if under contract or not";
   formvalid = false;
}else{

      if (contract =="YES"){
        //handle user input for contractname

         }
       }
if (formvalid==false){
return false;

  }
}

1 个答案:

答案 0 :(得分:0)

是的,我知道我做错了什么  document.getElementbyId(“ reportErr”)。innerHTML //错误,正在发布我的表单。 它应该是 document.getElementById()。innerHTML。 tnx JonP您的关注。您的线索帮助我完成了这项任务