Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Form Validation Problem
Old 11-24-2009, 12:00 AM Form Validation Problem
MZA
Experienced Talker

Posts: 47
Trades: 0
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..
MZA is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-24-2009, 05:38 AM Re: Form Validation Problem
Super Talker

Posts: 139
Name: John Davis
Trades: 0
Form code would help.
In any way there is too many misstakes.

1.
Code:
var selectBoxday = form.day; 
var bday=parseInt(selectBoxday.options[selectBoxday.selectedIndex].value);
equal to
Code:
var bday= form.day.value;
2. {} - excess
Code:
{
if((tmo > bmo)||(tmo==bmo & tday>=bday))
{age=byr}

else
{age=byr+1}
rlage=(tyr-age)
}}
3. You've missed {
Code:
if (rlage <= 18) (
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
MapMaster is offline
Reply With Quote
View Public Profile Visit MapMaster's homepage!
 
Reply     « Reply to Form Validation Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45832 seconds with 12 queries