...

View Full Version : Function Issues



tomyknoker
09-04-2007, 07:41 AM
Hi there can someone take alook at the following and tell me what I am doing wrong? Basically if a user selects checkbox 2 & checkbox 3 in my form and try to submit, the can only do it if they have also selected a radio button... Just not sure if my combination is wrong...


myRadio = -1;
for (i=form.radio1.length-1; i > -1; i--) {
if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1[i].checked) {
myRadio = i; i = -1;
}
}
if (myRadio == -1) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

Philip M
09-04-2007, 09:02 AM
for (i=form.radio1.length-1; i > -1; i--) {
if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1[i].checked) {
myRadio = i; i = -1;
}

form must be the actual name of the form. Or change to forms[0].

tomyknoker
09-04-2007, 09:10 AM
Hi Philip... Thanks got me slightly further with it, however now if checkbox1 is only checked, the alert still comes up... That the user has selected checkbox 2 & 3 and not selected a radio button... Any ideas?


myRadio = -1;
for (i=form.radio1.length-1; i > -1; i--) {
if((form.checkbox2.checked && form.checkbox3.checked) && form.radio1[i].checked) {
myRadio = i; i = -1;
}
}
if (myRadio == -1) {
theMessage = theMessage + "\n --> You selected checkbox 2 & 3, so you therefore need to select the radio button";
}

Philip M
09-04-2007, 09:16 AM
Parentheses:-

if (form.checkbox2.checked && form.checkbox3.checked && form.radio1[i].checked)

tomyknoker
09-04-2007, 09:25 AM
Hi Philip... My form is just getting more confusing... I don't know if I can explain it clearly but I'll try... They can select checkbox 1 & 3 and not have to select a radio button... They can select checkbox 1 alone and not have to select a radio button, the same goes for checkbox 3 alone... Only if they select checkbox 2, then they must select checkbox 3 and inturn means they also need the radio button checked... Argh it's a mess now do you know how I can get it all together doing those things?

tomyknoker
09-04-2007, 09:33 AM
This was my attempt as you can see it needs to be combined I think... Oh there is another condition but have no idea how to do it unless I add another 'if', if they select checkbox 1 then they can't select a radio button...


if(form.checkbox1.checked && form.checkbox2.checked)
{
theMessage = theMessage + "\n --> You can only choose either checkbox 1 or checkbox 2";
}

if(!(form.checkbox2.checked && form.checkbox3.checked) && !form.checkbox1.checked)
{
theMessage = theMessage + "\n --> You must select checkbox 1 or checkbox 2 and if 2 is selected checkbox 3 must also be checked";
}

myRadio = -1;
for (i=form.radio1.length-1; i > -1; i--) {
if ((form.checkbox2.checked & form.checkbox3.checked) && form.radio1[i].checked) {
myRadio = i; i = -1;
}
}
if (myRadio == -1) {
theMessage = theMessage + "\n --> You have select checkbox 2, which means you must have also selected checkbox 3, but haven't selected a radio button";
}

Kor
09-04-2007, 11:02 AM
Something like this?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function valid(f){
if(f['checkbox2'].checked&&!f['checkbox3'].checked){
alert('You should check also the checkbox 3.');
return false
}
else if(f['checkbox2'].checked&&f['checkbox3'].checked){
var rads=f['rad'], q=true, i=0, r;
while(r=rads[i++]){
if(r.checked){q=false;break}
}
if(q){
alert('You must check a radio button!');
return false
}
}
}
</script>
</head>
<body>
<form id="myform" action="" onsubmit="return valid(this)">
1. <input type="checkbox" name="checkbox1" value="1"><br>
2. <input type="checkbox" name="checkbox2" value="2"><br>
3. <input type="checkbox" name="checkbox3" value="3"><br>
-------------
<br>
<input type="radio" name="rad" value="1rad">
<input type="radio" name="rad" value="2rad">
<input type="radio" name="rad" value="3rad">
<input type="radio" name="rad" value="4rad">
<br>
-------------
<br>
<input type="submit" name="Sub" value="Submit">
</form>
</body>
</html>

Fang
09-04-2007, 11:15 AM
To validate the radio's:
if( (form.checkbox1.checked && form.checkbox3.checked) && !(form.radio1.checked || form.radio2.checked) )
{
theMessage = theMessage + "\n --> Please select a radio";
}
if( !(form.checkbox1.checked && form.checkbox3.checked) && (form.radio1.checked || form.radio2.checked) )
{
theMessage = theMessage + "\n --> Do not select a radio";
}

tomyknoker
09-04-2007, 01:07 PM
Kor how can I get that code into my already existing function, I'm not getting very far at all... This is what i have combined...


function validate() {

var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage

if(f['checkbox2'].checked&&!f['checkbox3'].checked){
theMessage = theMessage + "\n --> You should also check checkbox 3";
return false
}
else if(f['checkbox2'].checked&&f['checkbox3'].checked){
var rads=f['radio1'], q=true, i=0, r;
while(r=rads[i++]){
if(r.checked){q=false;break}
}
if(q){
theMessage = theMessage + "\n --> You must check a radio button";
return false
}
}
}

// make sure field is not blank
if (document.form.name.value=="") {
theMessage = theMessage + "\n --> Your name";
}

// make sure field is not blank
if (document.form.address1.value=="") {
theMessage = theMessage + "\n --> Your address";
}

// If no errors, submit the form
if (theMessage == noErrors) {
return true;

} else {

// If errors were found, show alert message
alert(theMessage);
return false;
}
}

tomyknoker
09-04-2007, 01:28 PM
I also tried the following way but it just ignores the 2 new functions completely...


function validate() {

var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage

if(form.checkbox1.checked && form.checkbox2.checked)
{
theMessage = theMessage + "\n --> You must either pick checkbox 1 or 2";
}

if(!(form.checkbox2.checked && form.checkbox3.checked) && !form.checkbox1.checked)
{
theMessage = theMessage + "\n --> Please choose either checkbox 1 or 2";
}

if( (document.form.checkbox1.checked && document.form.checkbox3.checked) && !(document.form.radio1.checked || document.form.radio2.checked) )
{
theMessage = theMessage + "\n --> Please select a radio";
}
if( !(document.form.checkbox1.checked && document.form.checkbox3.checked) && (document.form.radio1.checked || document.form.radio2.checked) )
{
theMessage = theMessage + "\n --> Do not select a radio";
}

Philip M
09-04-2007, 01:58 PM
function validate(f) {

It will never work unless you follow the advice you are given carefully.

It is asking for trouble to give a form the name "form", or a textbox name of "name".

tomyknoker
09-04-2007, 02:12 PM
Sorry it's my first attempt at JavaScript... Ok well the first part is working:


if(myform.checkbox2.checked&&! myform.checkbox3.checked){
theMessage = theMessage + "\n --> You should also check checkbox 3";
}

But the second isn't, the reason I removed the 'f' is because I have other things etc...


else if(myform.checkbox2.checked&& myform.checkbox3.checked){
var rads=myform.radio1, q=true, i=0, r;
while(r=rads[i++]){
if(r.checked){q=false;break}
}
if(q){
theMessage = theMessage + "\n --> You must check a radio button";
}
}
}

Kor
09-04-2007, 02:52 PM
How many radio buttons do you have? Can we see your HTML (or the significant part of it - check boxes and radio buttons)?

tomyknoker
09-04-2007, 02:58 PM
How many radio buttons do you have? Can we see your HTML (or the significant part of it - check boxes and radio buttons)?Of course here you go... Hopefully it makes a bit more sense now...


<form action="emailProcessor.php" onSubmit="return validate(this);" method="POST" name="myform">

<input type="checkbox" name="check1" id="checkbox1" value="value1">
<input type="checkbox" name="check2" id="checkbox2" value="value2">
<input type="checkbox" name="check3" id="checkbox3" value="value3">

<input type="radio" name="radio1" id="radio1" value="one">
<input type="radio" name="radio1" id="radio2" value="two">
<input type="radio" name="radio1" id="radio3" value="three">
<input type="radio" name="radio1" id="radio4" value="four">
<input type="radio" name="radio1" id="radio5" value="five">

Kor
09-04-2007, 03:45 PM
The problem is that you confound id with name

Classical javascript reference uses name, not id
document.forms['formname'].elements['elementname']


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function validate(f) {
var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage;
if(f['check2'].checked &&! f['check3'].checked){
theMessage = theMessage + "\n --> You should also check checkbox 3";
}

else if(f['check2'].checked && f['check3'].checked){
var rads=f['radio1'], q=true, i=0, r;
while(r=rads[i++]){
if(r.checked){q=false;break}
}
if(q){
theMessage = theMessage + "\n --> You must check a radio button";
}
}
if(theMessage!=noErrors){
alert(theMessage);
return false
}
}
</script>
</head>
<body>
<form action="" onsubmit="return validate(this)" name="myform">

<input type="checkbox" name="check1" id="checkbox1" value="value1">
<input type="checkbox" name="check2" id="checkbox2" value="value2">
<input type="checkbox" name="check3" id="checkbox3" value="value3">

<input type="radio" name="radio1" id="radio1" value="one">
<input type="radio" name="radio1" id="radio2" value="two">
<input type="radio" name="radio1" id="radio3" value="three">
<input type="radio" name="radio1" id="radio4" value="four">
<input type="radio" name="radio1" id="radio5" value="five">
<br>
<br>
<input type="submit" name="Sub" value="Submit">
</form>
</body>
</html>

tomyknoker
09-04-2007, 04:04 PM
Ok thanks for that I didn't realise I though ID was meant to be used, however it still hasn't fixed my issue with this line, can you see a reason why it wouldn't work? I'm not using 'f' but I am targeting the correct radio button am I not?


else if(document.myform.check2.checked&&document.myform.check3.checked){
var rads=document.myform.radio1, q=true, i=0, r;
while(r=rads[i++]){
if(r.checked){q=false;break}
}
if(q){
theMessage = theMessage + "\n --> You must check a radio button";
}
}
}

Is there a better way to do the radio button section??

Kor
09-04-2007, 04:28 PM
You have an extra bracket. Remove it (the red one):


else if(document.myform.check2.checked&&document.myform.check3.checked){
var rads=document.myform.radio1, q=true, i=0, r;
while(r=rads[i++]){
if(r.checked){q=false;break}
}
if(q){
theMessage = theMessage + "\n --> You must check a radio button";
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum