I have this basic script that I'm working with to require fields on a form...however, it's not working properly.
The required fields are name and email, and the OnClick on the submit is supposed to alert and stop the script from processing further if they're not filled in. However, what it is actually doing is alerting the message then processing the script anyway. I can't figure out what's wrong with it!
Here's the code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.subscribe.names.value=="") {
themessage = themessage + " - Name";
}
if (document.subscribe.email.value=="") {
themessage = themessage + " - E-mail";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
return false;
//document.subscribe.submit();
}
else {
alert(themessage);
return false;
}
}
// End -->
</script>
....................................and the form.................
<form action="processmail.php" method="post" name="subscribe" id="subscribe">
<p>Name<br>
<input type="text" name="names" maxlength="50">
<br>
Email<br>
<input type="text" name="email" maxlength="50"><br>
<input type="radio" name="subscrib" value=http://www.webdeveloper.com/forum/archive/index.php/"Sub" checked><b>Subscribe</b> <input type="radio" name="subscrib" value="Un"><b>Unsubscribe</b><br>
<input type="submit" name="subscribe_submit" id="subscribe_submit" value="Subscribe" class="button" onClick="verify();"></p>
</form>
Thanks!!
The required fields are name and email, and the OnClick on the submit is supposed to alert and stop the script from processing further if they're not filled in. However, what it is actually doing is alerting the message then processing the script anyway. I can't figure out what's wrong with it!
Here's the code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.subscribe.names.value=="") {
themessage = themessage + " - Name";
}
if (document.subscribe.email.value=="") {
themessage = themessage + " - E-mail";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
return false;
//document.subscribe.submit();
}
else {
alert(themessage);
return false;
}
}
// End -->
</script>
....................................and the form.................
<form action="processmail.php" method="post" name="subscribe" id="subscribe">
<p>Name<br>
<input type="text" name="names" maxlength="50">
<br>
Email<br>
<input type="text" name="email" maxlength="50"><br>
<input type="radio" name="subscrib" value=http://www.webdeveloper.com/forum/archive/index.php/"Sub" checked><b>Subscribe</b> <input type="radio" name="subscrib" value="Un"><b>Unsubscribe</b><br>
<input type="submit" name="subscribe_submit" id="subscribe_submit" value="Subscribe" class="button" onClick="verify();"></p>
</form>
Thanks!!