View Full Version : Nested if statements
cree2me
11-06-2003, 09:07 PM
Hey
I got part of my code to work right. If a user selects a Class (check box), but forgets to select a start date. My code prompts the user to select a start date from a drop down list.
The problem is, if the user does not select a Class, the prompt is to select a start date is still activated. Even though it's only an option.
Here is my code:
<html>
<body>
<script type="text/javascript">
function validate()
{
x=document.myForm
at=x.myEmail.value.indexOf("@")
code=x.myCode.value
firstname=x.myName.value
cs=x.comm.value
csstart=x.csstarted.value
submitOK="True"
if (at==-1)
{
alert("Not a valid e-mail")
submitOK="False"
}
if (code<1 || code>5)
{
alert("Your code must be between 1 and 5")
submitOK="False"
}
if (firstname.length>10)
{
alert("Your name must be less than 10 letters")
submitOK="False"
}
if (cs.length<1 || csstart.length<1)
{
alert("Please select a Communication Skills start date.")
submitOK="False"
}
if (submitOK=="False")
{
return false
}
}
</script>
</head>
<body>
<form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()">
Enter your e-mail:
<input type="text" name="myEmail">
<br>
Enter your code, value from 1 to 5:
<input type="text" name="myCode">
<br>
Enter your first name, max 10 letters:
<input type="text" name="myName">
<br>
Communication Skills
<input name="comm" type="checkbox" value="1">
<br>
Start Date
<select name="csstarted" size="1"><option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option></select>
<input type="submit" value="Send input">
</form>
</body>
</html>
Any help would be appreciated.
Glen
Basscyst
11-06-2003, 09:37 PM
Hello -
I'm not sure if I understand but . . .Change this:
if (cs.length<1 || csstart.length<1)
{
alert("Please select a Communication Skills start date.")
submitOK="False"
}
to 2 different statements:
if (cs.length<1)
{
alert("Please select a Communication Skills start date.")
submitOK="False"
}
else if (csstart.length<1)
{
alert("Please select a Communication Skills start date.")
submitOK="False"
}
Hope That Helps,
Basscyst
cree2me
11-06-2003, 10:08 PM
Thanks,
I tried the code, but the 'Please select a start date' still pops up. If the user does not select this check box, they are not asked to select a start date for the class.
Basscyst
11-06-2003, 10:19 PM
OK - I see. Try This.
<html>
<body>
<script type="text/javascript">
function validate()
{
x=document.myForm
at=x.myEmail.value.indexOf("@")
code=x.myCode.value
firstname=x.myName.value
cs=x.comm.value
vcsstart=x.csstarted.value
submitOK="True"
if (at==-1)
{
alert("Not a valid e-mail")
submitOK="False"
}
if (code<1 || code>5)
{
alert("Your code must be between 1 and 5")
submitOK="False"
}
if (firstname.length>10)
{
alert("Your name must be less than 10 letters")
submitOK="False"
}
if(document.myForm.comm.checked==true)
{
if (cs.length<1 || csstart.length<1)
{
alert("Please select a Communication Skills start date.")
submitOK="False"
}
}
if (submitOK=="False")
{
return false
}
}
</script>
</head>
<body>
<form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()">
Enter your e-mail:
<input type="text" name="myEmail">
<br>
Enter your code, value from 1 to 5:
<input type="text" name="myCode">
<br>
Enter your first name, max 10 letters:
<input type="text" name="myName">
<br>
Communication Skills
<input name="comm" type="checkbox" value="1">
<br>
Start Date
<select name="csstarted" size="1"><option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option></select>
<input type="submit" value="Send input">
</form>
</body>
</html>
glenngv
11-07-2003, 04:45 AM
You want to check if the checbox is checked not if it has a value (which it always has even if it's unchecked!)
function validate()
{
var x=document.myForm;
if (x.myEmail.value.indexOf("@")==-1) //should use regexp for stricter validation
{
alert("Not a valid e-mail");
x.myEmail.focus();
return false;
}
var code=x.myCode.value;
if (code<1 || code>5)
{
alert("Your code must be between 1 and 5");
x.myCode.focus();
return false;
}
if (x.myName.value.length>10)
{
alert("Your name must be less than 10 letters");
x.myName.focus();
return false;
}
if (x.comm.checked && x.csstarted.options[x.csstarted.selectedIndex].value=="")
{
alert("Please select a Communication Skills start date.");
x.csstarted.focus();
return false;
}
return true;
}
adios
11-07-2003, 04:50 AM
function validate(oForm)
{
var sMsg = '';
if (oForm.myEmail.value.indexOf('@') == -1)
sMsg += 'Not a valid e-mail.\n';
var code = oForm.myCode.value;
if (code != '' && (isNaN(code) || parseInt(code, 10) < 1 || parseInt(code, 10) > 5))
sMsg += 'Your code must be between 1 and 5.\n';
if (oForm.myName.value.length > 10)
sMsg += 'Your name must be less than 10 letters.\n';
if (oForm.comm.checked && oForm.csstarted.selectedIndex == 0)
sMsg += 'Please select a Communication Skills start date.\n';
if (sMsg != '')
{
sMsg = 'The following errors must be corrected before submitting:\n\n' + sMsg + '\nThank you.';
alert(sMsg);
return false;
}
return true;
}
<form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate(this)">
cree2me
05-27-2004, 06:20 PM
Sorry for the delay in replying, other work duties took over. I really appreciate your input. Thanks guys! :cool:
cree2me
07-14-2004, 09:00 PM
Thanks for all the help!
I finally got the pop-up messages to work if they select a course option button, but forget to select a start date.
Now I need help with on how to calculate the tuition total. I understand that I can use a hidden button for these, since all courses are one cost. Should I use if statements when the Course option is checked and the start date is also non-blank.
Here is my code:
<html>
<body>
<script type="text/javascript">
function validate()
{
var x=document.myForm;
if (x.myEmail.value.indexOf("@")==-1) //should use regexp for stricter validation
{
alert("Not a valid e-mail");
x.myEmail.focus();
return false;
}
var code=x.myCode.value;
if (code<1 || code>5)
{
alert("Your code must be between 1 and 5");
x.myCode.focus();
return false;
}
if (x.myName.value.length>10)
{
alert("Your name must be less than 10 letters");
x.myName.focus();
return false;
}
if (x.comm.checked && x.csstarted.options[x.csstarted.selectedIndex].value=="")
{
alert("Please select a Communication Skills start date.");
x.csstarted.focus();
return false;
}
if (x.ps.checked && x.psstarted.options[x.psstarted.selectedIndex].value=="")
{
alert("Please select a Problem Solving start date.");
x.psstarted.focus();
return false;
}
if (x.ep.checked && x.epstarted.options[x.epstarted.selectedIndex].value=="")
{
alert("Please select a Educating the Public start date.");
x.epstarted.focus();
return false;
}
if (x.cd.checked && x.cdstarted.options[x.cdstarted.selectedIndex].value=="")
{
alert("Please select a Career Development start date.");
x.cdstarted.focus();
return false;
}
if (x.ms.checked && x.msstarted.options[x.msstarted.selectedIndex].value=="")
{
alert("Please select a Management Skills start date.");
x.msstarted.focus();
return false;
}
if (x.pi.checked && x.pistarted.options[x.pistarted.selectedIndex].value=="")
{
alert("Please select a Project Implementation start date.");
x.pistarted.focus();
return false;
}
if (x.pdd.checked && x.pddstarted.options[x.pddstarted.selectedIndex].value=="")
{
alert("Please select a Program Design and Development start date.");
x.pddstarted.focus();
return false;
}
if (x.pl.checked && x.plstarted.options[x.plstarted.selectedIndex].value=="")
{
alert("Please select a Policy and Legislation start date.");
x.plstarted.focus();
return false;
}
return true;
}
function sumIt(obj) {
total = 0.00;
for (i=0; i<obj.fees.length; i++) {
if(obj.fees[i].checked == true){
total+=parseFloat(obj.fees[i].value);
}
}
total = Math.round(total*100).toString();
total = total.substring(0,total.length-2)+'.'+
total.substring(total.length-2,total.length);
obj.totalman.value=('$'+total);
}
</script>
</head>
<form name="myForm" action="tryjs_submitpage.htm" onsubmit="return validate()">
Enter your e-mail:
<input type="text" name="myEmail">
<br>
Enter your code, value from 1 to 5:
<input type="text" name="myCode">
<br>
Enter your first name, max 10 letters:
<input type="text" name="myName">
<br>
<table width="572" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>Module</td>
<td>Start Date</td>
</tr>
<tr>
<td width="267"> <input name="comm" type="checkbox" value="400.00" onclick="sumIt(this.form)">
Communication Skills </td>
<td width="298"><select name="csstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="ps" type="checkbox" value="1">
Problem Solving </td>
<td><select name="psstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="ep" type="checkbox" value="1">
Educating the Public</td>
<td><select name="epstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="cd" type="checkbox" value="1">
Career Development </td>
<td><select name="cdstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="ms" type="checkbox" value="1">
Management Skills </td>
<td><select name="msstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="pi" type="checkbox" value="1">
Project Implementation </td>
<td><select name="pistarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="pdd" type="checkbox" value="1">
Program Design and Development </td>
<td><select name="pddstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td>
<input name="pl" type="checkbox" value="1">
Policy and Legislation </td>
<td><select name="plstarted" size="1">
<option value=""> </option>
<option value="2">Jan 5 - Feb 27/04</option>
<option value="3">Feb 9 - Apr 2/04</option>
</select></td>
</tr>
<tr>
<td><div align="right">Tuition Total</div></td>
<td><input name="totalman" type="text" READONLY></td>
</tr>
</table>
<br>
<input type="submit" value="Send input">
</form>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.