使用Javascript函数自动提交表单以进行验证

时间:2011-09-13 14:43:43

标签: javascript

我有一个表单,当用户点击提交按钮时,运行这个javascript以验证填写了必填字段:

<script type="text/javascript">
function validateForm()
{
var x=document.forms["OrderPage"]["client_name"].value
if (x==null || x=="")
  {
  alert("Client Name is required.");
  return false;
  }
var a=document.forms["OrderPage"]["client_email"].value
if (a==null || a=="")
  {
  alert("Client Email is required.");
  return false;
  }
var b=document.forms["OrderPage"]["client_home_phone"].value
if (b==null || b=="")
  {
  alert("Client Home Phone is required.");
  return false;
  }
var c=document.forms["OrderPage"]["site_street"].value
if (c==null || c=="")
  {
  alert("Site Street is required.");
  return false;
  }
}
</script>

我现在想要添加的是每10分钟自动提交表单的javascript,但我想确保它验证填写了必填字段,否则我不希望提交表单,我希望弹出警报。我有以下代码,它正在提交表单,但它没有调用validateForm函数:

<SCRIPT LANGUAGE="JavaScript"><!--
setTimeout('document.OrderPage.submit()',50000);
//--></SCRIPT>

这是我在页面上的按钮,如果用户点击它,它会调用该函数来验证这些字段:

<form name="OrderPage" action="save_client_information.php" onsubmit="return validateForm()" method="post">

<input name="save_client_info" type="submit" value="Save" />

任何帮助组合自动保存和验证表单的功能将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

为什么要以给定的间隔提交。为什么不在间隔时间调用validate方法。

setInterval(validateForm,50000);

确保setInterval方法在验证时暂停,否则会看到很多恼人的警报消息