0

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

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>
4

1 回答 1

0

A quick-fix:

<input type="submit" onclick="return (validate() && confirm('Do you really want to continue?'))" value="Send" />
于 2013-11-08T20:58:41.480 回答