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.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    HTML value for checkbox?

    I know this is basic Javascript, but I just cant seem to understand. The following code is supposed to validate if at least one of the three checkboxes has been selected. But the way I am going about checking for the value is not working. What am I doing wrong? (the alerts are just for testing)

    f0 = 0
    f1 = 1
    f2 = 2

    errMsg = "The value of Pain is:"+document.pagefive.elements['Pain'].value;
    alert(errMsg);

    errMsg = "The value of Tingling is:"+document.pagefive.elements['Tingling'].value;
    alert(errMsg);

    errMsg = "The value of Numbness is:"+document.pagefive.elements['Numbness'].value;
    alert(errMsg);

    if ((frm.elements[f0].value =~ "Yes") &&
    (frm.elements[f1].value =~ "Yes") &&
    (frm.elements[f2].value =~ "Yes"))

    {

    Thank you!!!
    -tdavis

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    function CheckIt(){
    nc=0;
    numCheck=3;
    for (i=0;i<=numCheck-1;i++) {
    if (document.frm.["f"+i].checked==true) {nc++}
    }
    if (nc==0) alert ('No boxes were checked');
    }

    Not sure what
    if ((frm.elements[f0].value =~ "Yes") &&
    (frm.elements[f1].value =~ "Yes") &&
    (frm.elements[f2].value =~ "Yes"))
    is supposed to do, but =~ is not valid JavaScript syntax. If it is meant to mean the string contains or includes the characters "Yes" then that is PERL.

  • #3
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! I am trying that now. I keep getting OJBECT EXPECTED error though. I see that all the time and I cant understand what exactly is missing. This language is harder than any other I've used!!!

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    How are you calling CheckIt()? Is it being called from the onsubmit on the form tag or the onclick from an input button tag?

    What are the names of the checkbox fields? Are they named f0, f1, f2?
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    The checkbox field names are Pain, Tingling, and Numbness. Here is my script now (see below). It seems to work, but it is not elegant. It is called with ONCLICK on an image. I just now added the frm.submit(), and I guess that gets the CGI to execute when one of the three checkboxes is clicked. I hard coded the fields because I could not figure it out otherwise.

    Thanks for your help!
    -tdavis


    function validatePage5Form(frm)
    {

    var errMsg = "You did not make a selection.\nPlease choose from one of the following:\nPain, Tingling, Numbness";
    var nc=0;

    if (document.pagefive.elements.Pain.checked==true) {
    nc++;
    }

    if (document.pagefive.elements.Tingling.checked==true) {
    nc++;
    }

    if (document.pagefive.elements.Numbness.checked==true) {
    nc++;
    }

    if (nc==0) alert(errMsg);
    else frm.submit();

    }

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Try replacing document.pagefive.elements.Pain.checked with frm.Pain.checked and similarly for the other two fields.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tony Davis
    Thanks! I am trying that now. I keep getting OJBECT EXPECTED error though. I see that all the time and I cant understand what exactly is missing. This language is harder than any other I've used!!!
    Hey Tony,

    From the school of hard knocks, I have come to understand the error OJBECT EXPECTED means you made a call or used an OJBECT that was not define. I usually see this when I have called a javascript function() and I didn't quite spelled the name of the function the same as I define it. Remember case rule applies.

    PhotoJoe

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks very much for all the help!!!


  •  

    Posting Permissions

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