How to check for empty fields?

345d54 25 Jul, 2007
Hello,

I want to check that there are no empty fields when a guest submits the form.
My form is called "contact" and until now looks something like this:

<?php global $my; $mainframe; ?>
<p>Name: <input type="text" name="name" value="<?php echo $my->username; ?>" /></p> 
<p>Email: <input type="text" name="email" value="<?php echo $my->email; ?>" /></p>
<p>Telefon: <input type="text" name="Telefon"></p>
<p>Betreff: <input type="text" name="betreff"></p>
<p>Nachricht: <textarea name="nachricht" cols="30" rows="5" wrap="virtual"></textarea> </p>
<p><input type="submit" name="Submit" value="Senden"></p>
<p><input type="hidden" name="title" value="<?php echo $mainframe->_head['title']; ?>" />


In the general settings of the form I filled in

onsubmit=return validateForm()
in the row:

Form tag attachment:
something like onSubmit()

Now I guess I need to fill in the form code field:

On Submit code - before sending email:
(PHP code with tags)

the script: validateForm (which I lack at the moment and have no clue how it should look like=)

is there anyone who knows?

THanks in advance
Hanne
GreyHead 26 Jul, 2007
Hi Hanne,

Please have a look at this post which has a simple form validation example telling you how to do this (at least for a single field).

Bob<br><br>Post edited by: GreyHead, at: 2007/08/11 15:01
345d54 26 Jul, 2007
thank you for the hint - it works fine.



 function checkForm(form)
{
 if(form.name.value == ""«»)  {
   alert("Please enter your name!"«»);
   return false;
  }
 return true;
}



but now: how can I add another field?



 function checkForm(form)
{
 if(form.name.value == ""«»)  {
   alert("Please enter your name!"«»);
   return false;
  }
 return true;

 if(form.email.value == ""«»)  {
   alert("Please enter your Email!"«»);
   return false;
  }
 return true;
}



I read something about && but I can´t find out about the syntax, I am doing trial and error fun stuff for about 2hours now and I guess it´s just peanuts for you - so have mercy!<br><br>Post edited by: 345d54, at: 2007/07/25 22:41
GreyHead 26 Jul, 2007
Hi 345d54,

One way is to stack up the if clauses like:
 function checkForm(form)
{
if(form.name.value == ""«») {
   alert("Please enter your name!"«»);
   return false;
} else if (form.email.value == ""«») {
   alert("Please enter your Email!"«»);
   return false;
}
   return true;
}


Not tested but should work.

Bob
345d54 26 Jul, 2007
It works just fine!

Thank you Bob - the support in this Forum is amazing.
lydbury 10 Oct, 2007
That's exactly what I want to do but cannot make it work. Am I correct that I leave the script exactly as it is (just change the name of the filed) and then paste into the html or should it go in the javascript box?

Then does the call stay as it is?

Do I need to change the form name at all.

Grateful for your help.

Duncan
GreyHead 10 Oct, 2007
Hi Duncan,

In the latest version of ChronoForms you can set up field validation from the Form Manager. Just enter the names of the required fields in the Validation tab.

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger