Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Function Issues

  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Function Issues

    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...

    Code:
    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";	
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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].

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

    Code:
    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";	
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Parentheses:-

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

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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...

    Code:
    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";	
    }

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Something like this?
    Code:
    <!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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    To validate the radio's:
    Code:
    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";
        }

  • #9
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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;
    }
    }
    Last edited by tomyknoker; 09-04-2007 at 12:19 PM.

  • #10
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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";
    }

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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".

  • #12
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry it's my first attempt at JavaScript... Ok well the first part is working:

    Code:
    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...

    Code:
    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";
    }
    }
    }

  • #13
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    How many radio buttons do you have? Can we see your HTML (or the significant part of it - check boxes and radio buttons)?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #14
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    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...

    Code:
    <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">

  • #15
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The problem is that you confound id with name

    Classical javascript reference uses name, not id
    document.forms['formname'].elements['elementname']
    Code:
    <!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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •