当要求继续表单提交时,表单发送按钮不会验证表单输入文本字段

时间:2013-11-08 20:45:38

标签: javascript html forms

我在www.thetotempole.ca的网页上有一个表单,例如它有一个邮政/邮政编码文本字段,如果用户没有输入正确的邮政编码,表单会显示一个弹出文件说明邮政编码无效。虽然,当用户点击提交时,我希望它显示确认弹出,询问他们是否真的想要发送表单。这会干扰输入文本字段验证(I.E邮政编码验证器)一些如何以及最终根本不验证文本字段。任何帮助表示赞赏。谢谢!!

HTML:

<html>
   <head>
      <script>
         function validate()
         {
            var myform = document.getElementById("form1");
            myform.fname.style.backgroundColor = "white";
            myform.lname.style.backgroundColor = "white";
            myform.address.style.backgroundColor = "white";
            myform.city.style.backgroundColor = "white";
            myform.provstate.style.backgroundColor = "white";
            myform.country.style.backgroundColor = "white";
            myform.postalzip.style.backgroundColor = "white";
            console.log("form object: " + myform);
            if (myform.fname.value == "")
            {
               alert("First Name must have a value");
               myform.fname.focus();
               myform.fname.style.backgroundColor = "red";
               return false;
            }
            if (myform.lname.value == "")
            {
               alert("Last Name must have a value");
               myform.lname.focus();
               return false;
            }
            if (myform.address.value == "")
            {
               alert("Address must have a value");
               myform.address.focus();
               return false;
            }
            if (myform.postalzip.value == "")
            {
               alert("Postal/Zip Code must have a value");
               myform.postalzip.focus();
               return false;
            }
            else
            {
               //var regex = new RegExp();
               var regex = /^([a-z]\d[a-z]\s?\d[a-z]\d)|(\d{5}(\s?\d{4})?)$/i;
               myform.postalzip.value.toUpperCase();
               if (!regex.test(myform.postalzip.value))
               {
                  alert("Postal/Zip Code has invalid format");
                  myform.postalzip.focus();
                  return false;

               }
            }

            return true;
         }
      </script>
   </head>
   <body>
      <form id="form1">
         First Name:<input type="text" name="fname" /><br />
         Last Name:<input type="text" name="lname" /><br />
         Address:<input type="text" name="address" /><br />
         City:<input type="text" name="city" /><br />
         Province/State:<input type="text" name="provstate" /><br />
         Country:<input type="text" name="country" /><br />
         Postal/Zip Code:<input style="text-transform: uppercase;" type="text" name="postalzip" /><br />
         <input type="submit" onclick="return confirm('Do you really want to continue?')" value="Send" />
         <input type="reset" onclick="return confirm('Do you really want to reset?')" value="Reset" />

      </form>
   </body>
</html>

1 个答案:

答案 0 :(得分:0)

快速修复:

<input type="submit" onclick="return (validate() && confirm('Do you really want to continue?'))" value="Send" />