简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM