bcampbell
01-27-2009, 09:54 PM
Hi all, I am new to Javascript and was seeking out help to a page I am going to try and build. I am creating a form with 2 text boxes, and I want to be able to validate and make sure that if either 1 has data inside, to check the other to make sure that it must have data as well, and error out if not. I found this code online, any help with making it the way I want it would be fantastic. Thanks much in advance.
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.first.value!="") {
themessage = themessage + " - First Name";
}
if (document.form.last.value=="") {
themessage = themessage + " - Last Name";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
<form name=form method="post" action="">
<input type=text name="first" size="20"> First Name<BR>
<input type=text name="last" size="20"> Last Name<BR>
<input type=button value="Submit Request" onclick="verify();">
<input type=reset value="Clear Form"><br>
</form>
I am sure this is an easy fix, but like I said, I am new to js and any help would be greatly appreciated.
Thanks much!
bcampbell
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.first.value!="") {
themessage = themessage + " - First Name";
}
if (document.form.last.value=="") {
themessage = themessage + " - Last Name";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
<form name=form method="post" action="">
<input type=text name="first" size="20"> First Name<BR>
<input type=text name="last" size="20"> Last Name<BR>
<input type=button value="Submit Request" onclick="verify();">
<input type=reset value="Clear Form"><br>
</form>
I am sure this is an easy fix, but like I said, I am new to js and any help would be greatly appreciated.
Thanks much!
bcampbell