...

View Full Version : How do I verify the fields are filled in before the form action



Typhoid Timmy
12-19-2006, 06:12 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<SCRIPT LANGUAGE="JavaScript">
function verify() {
var themessage = "You are required to complete at least these following fields: ";
if (document.form.first_name.value=="") {
themessage = themessage + " - First Name";
}
if (document.form.last_name.value=="") {
themessage = themessage + " - Last Name";
}
if (document.form.email.value=="") {
themessage = themessage + " - E-mail";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete at least these following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
// End -->
</SCRIPT>
</head>

<body>



<form name=form method="post" action="http://www.s.com/servlet/servlet.WebToLead?encoding=UTF-8" onSubmit="return validateForm(this.submit)">
<input type=hidden name="oid" value="00D300000000YKF">
<input type=hidden name="retURL" value="http://www.s.com/thanks.html">


<table width="100%" border="0">
<tr>
<td width="21%"><font face="Arial, Helvetica, sans-serif">

<label for="first_name">First Name:</label>
</font></td>
<td width="79%"><font face="Arial, Helvetica, sans-serif">
<input maxlength="40" type="text" id="first_name" size="40" name="first_name">
<label for="label3"></label>
</font><font face="Arial, Helvetica, sans-serif">
<label for="label4"></label>
<label for="label5"></label>
</font></td>
</tr>
<tr>
<td><font face="Arial, Helvetica, sans-serif">
<label for="last_name">Last Name:</label>

</font></td>
<td><font face="Arial, Helvetica, sans-serif">
<input maxlength="40" type="text" id="last_name" size="40" name="last_name">
</font></td>
</tr>
<tr>
<td height="24"><font face="Arial, Helvetica, sans-serif">
<label for="label3">Email:</label>
</font></td>
<td><font face="Arial, Helvetica, sans-serif">
<input maxlength="80" type="text" id="email" size="60" name="email" />
</font></td>
</tr>
</table>
<p><br>
<input type="submit" name="submit" onclick="verify()">
</form>
</body>
</html>

The script is working correctly and coming up with a warning that these fields need to be filled but I cannot prevent it from sending the form regardless because of the action. I know I am missing something overly stupid here but I am sorta new making scripts. Anyone spare a moment and correct me where I need correcting?

david_kw
12-19-2006, 07:26 PM
First of all you don't need the onclick for the submit button

<input type="submit" name="submit" onclick="verify()">

to

<input type="submit" name="submit" />

Second you need to call the right function in your form onsubmit

<form name=form method="post" action="http://www.s.com/servlet/servlet.WebToLead?encoding=UTF-8" onSubmit="return validateForm(this.submit)">

to

<form name=form method="post" action="http://www.s.com/servlet/servlet.WebToLead?encoding=UTF-8" onSubmit="return veryify()">

That should get you further.

david_kw

david_kw
12-19-2006, 07:30 PM
Also you should be able to turn

document.form.submit();

in to

return(true);

david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum