I'm using javascript to validate a form I have, but when I submit a blank form it goes right on to the next page, so I'm guessing one of my conditions is messed up and is returning true?
Here is my script in the header:
Code:
<script type="text/javascript">
function Age()
{
var selectBoxday = form.day;
var bday=parseInt(selectBoxday.options[selectBoxday.selectedIndex].value);
var selectBoxmo = form.month;
var bmo=(parseInt(selectBoxmo.options[selectBoxmo.selectedIndex].value)-1);
var selectBoxyr = form.year;
var byr=parseInt(selectBoxyr.options[selectBoxyr.selectedIndex].value);
var byr;
var age;
var rlage;
var now = new Date();
tday=now.getDate();
tmo=(now.getMonth());
tyr=(now.getFullYear());
{
if((tmo > bmo)||(tmo==bmo & tday>=bday))
{age=byr}
else
{age=byr+1}
rlage=(tyr-age)
}}
function checkRadio(field) {
for(var i=0; i < field.length; i++) {
if(field[i].checked) return field[i].value;
}
return false; }
function checkForm(form) {
// validation fails if the input is blank
if(form.name.value == '') {
alert("You have to enter your name.");
form.name.focus();
return false; }
else {return true;}
if(form.nickname.value == '') {
alert("You have to enter a model name.");
form.nickname.focus();
return false; }
else {return true;}
// regular expression to match only alphanumeric characters and dash
var nn = /^[\w\-]+$/;
if(!nn.test(form.nickname.value)) {
alert("Model name can only contain numbers, letters, underscores(_), and dashes(-).");
form.nickname.focus();
return false; }
else {return true;}
if(form.email.value == '') {
alert("You have to enter your email address.");
form.email.focus();
return false; }
else {return true;}
var em = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(!em.test(form.email.value)) {
alert("Invalid email address.");
return false; }
else {return true;}
if(form.email.value != form.emailcheck.value) {
alert("Your email does not match.");
form.email.focus();
return false; }
else {return true;}
if(form.password.value == '') {
alert("You have to choose a password.");
form.password.focus();
return false; }
else {return true;}
var pw = /^[\w\-]+$/;
if(!pw.test(form.password.value)) {
alert("Password can only contain numbers, letters, underscores(_), and dashes(-).");
form.password.focus();
return false; }
else {return true;}
if((form.month.selectedIndex == 0) && (form.day.selectedIndex == 0) && (form.year.selectedIndex == 0) {
alert("Choose your birth date from the dropdown boxes.");
form.month.focus();
return false; }
else {return true;}
if (rlage <= 18) (
alert("You must be 18 to apply.");
form.year.focus();
return false; }
else {return true;}
if(form.description.value == '') {
alert("You have to enter a description.");
form.description.focus();
return false; }
else {return true;}
if(radioValue != checkRadio(form.nude)) {
alert("You must select an option.");
form.nude.focus();
return false; }
else {return true;}
if(form.feet.value == '') {
alert("You have to enter your height.");
form.feet.focus();
return false; }
else {return true;}
if(form.inches.value == '') {
alert("You have to enter your height.");
form.inches.focus();
return false; }
else {return true;}
if(form.weight.value == '') {
alert("You have to enter your weight.");
form.weight.focus();
return false; }
else {return true;}
if(form.hair.value == '') {
alert("Enter hair color/type.");
form.hair.focus();
return false; }
else {return true;}
if(form.eyes.value == '') {
alert("Enter your eye color.");
form.eyes.focus();
return false; }
else {return true;}
if(form.alt.value == '') {
alert("You cannot leave this field blank.");
form.alt.focus();
return false; }
else {return true;}
if(form.why.value == '') {
alert("You cannot leave this field blank.");
form.why.focus();
return false; }
else {return true;}
}
</script>
And this is in the body:
HTML Code:
<form action="process.php" method="post" onSubmit="return checkForm(this);">
Last edited by MZA; 11-24-2009 at 12:26 AM..
|