开发者

Auto-Submit Form with Javascript function to validate

I have a form, when the user clicks on the submit button this javascript is ran to validate the required fields are filled out:

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

What I am trying to add now is javascript to auto-submit the form every 10 min, but I want to make sure that it validates those required fields are filled in, otherwise I don't want the form to be submitted, and I want the alert to pop up. I have 开发者_运维百科the following code, and it is submitting the form, but it doesn't call the function to validateForm:

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

And this is my button on the page, where if the user clicks it, it calls the function to validate those fields:

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

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

Any help combining the functions to auto-save and validate the form would be greatly appreciated! Thank you!


Why submit at all at given interval. Why not just call validate method at interval.

setInterval(validateForm,50000);

Ensure setInterval method is paused when it is already validating, otherwise you will see lot of annoying alert messages

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜