SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast casa's Avatar
    Join Date
    Oct 2002
    Location
    Alabama
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript validataion question/problem

    I am dynamically buiding an html form with php. I'm reading a db table field which has a set of codes delimited by a pipe. If the code is 1-2 characters long it's a group and it it's 3-4 characters long it's a deparment within a particular group. My form goes something like this:
    Code:
    ----------------------------------------------------- 
    Date Range: [ Select a Date Range  |v]
    
    Group or Department: 
    	   [ ] AD		  select as 
    	   [ ] BD		  many as
    	   [ ] CD01		needed
    	   [ ] CD02
    	   [ ] CD10
    -----------------------------------------------------
    		 [ Select ] [ Reset ]
    -----------------------------------------------------
    In this case, I would have two hidden fields,
    Code:
    <input type="hidden" name="GR" value=2>
    <input type="hidden" name="DP" value=3>
    so I would know how many possible fields there would be.

    The user could select all or any combination of these 5 checkboxes, however, they have to select at least on. Right now, I have them set like this:
    Code:
    <input type="checkbox" name="G1" value="AD">
    <input type="checkbox" name="G2" value="BD">
    <input type="checkbox" name="D1" value="CD01">
    <input type="checkbox" name="D2" value="CD02">
    <input type="checkbox" name="D3" value="CD10">
    I can't figure out how to validate this correctly. I was thinking about something like:
    Code:
    <input type="checkbox" name="G[1]" value="AD">
    <input type="checkbox" name="G[2]" value="BD">
    <input type="checkbox" name="D[1]" value="CD01">
    <input type="checkbox" name="D[2]" value="CD02">
    <input type="checkbox" name="D[3]" value="CD10">
    But am not sure exactly how that works. Any pointers or advice would be greatly appreciated.

    Thanks,
    casa

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    If all you need is to check for one or more selections, name all the checkboxes with the same name and use:

    Code:
    var c = document.getElementsByName( '<name>' );
    and iterate through the resulting array of checkboxes looking for at least one that is c[i].checked == true.

    if you need to retain the unique names, give each the same id, and use the same line of code to retrieve the array of cboxes.

    This will only work in IE, to my knowledge

  3. #3
    SitePoint Enthusiast casa's Avatar
    Join Date
    Oct 2002
    Location
    Alabama
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to keep the groups and departments seperate, because of what I have to do with the information when I hit submit and go to the next script, BUT I can do something like this:
    Code:
    var hits=0;
    for (i=1;i<=Gboxes;i++) {
      if (g[i].checked == true) { hits++; }
    }
    for (i=1;i<=Dboxes;i++) {
      if (d[i].checked == true) { hits++; }
    }
    if (hits == 0) {
      alert("make a selection");
      return false;
    }
    Do I have it right?
    Would the html look like this:
    Code:
    <input type="checkbox" name="G[1]" value="AD">
    <input type="checkbox" name="G[2]" value="BD">
    <input type="checkbox" name="D[1]" value="CD01">
    <input type="checkbox" name="D[2]" value="CD02">
    <input type="checkbox" name="D[3]" value="CD10">
    or this:
    Code:
    <input type="checkbox" name="G[]" value="AD">
    <input type="checkbox" name="G[]" value="BD">
    <input type="checkbox" name="D[]" value="CD01">
    <input type="checkbox" name="D[]" value="CD02">
    <input type="checkbox" name="D[]" value="CD10">
    ???
    BTW, the PHP for building the checkboxes currently looks like this:
    PHP Code:
    <?php
    $depts 
    explode("|",$row['depts']);
    reset ($orgs);
    $g 0;
    $d 0;
    foreach (
    $depts as $value) {
      if (
    strlen($value) > 2) {
        
    $g++;
        
    $dispG .= "<input type=\"checkbox\" name=\"G".$g."\" value=\"".$value."\">";
        
    $dispG .= "&amp;nbsp;".$value."<br />\n";
      } elseif (
    strlen($value) <= 2) {
        
    $d++;
        
    $dispD .= "<input type=\"checkbox\" name=\"D".$d."\" value=\"".$value."\">";
        
    $dispD .= "&amp;nbsp;".$value."<br />\n";
      }
    }
    ?>
    Thanks for the help...

  4. #4
    SitePoint Enthusiast casa's Avatar
    Join Date
    Oct 2002
    Location
    Alabama
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I was a bit unclear on that. I started out in the mid/late 70's, early 80's programming Fortran, then COBOL, then took a 17 yr sebatical from programming and computers. Well, sort of, I did get a MIS degree in there somewhere where I took a couple of Pascal courses. I really understood arrays and even did some project using 3 dimensions, but times, they are a changin'. Some of these new fangled concepts take a bit of explaining for me to get a handle on them...

  5. #5
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    just to clarify

    name='G[1]' doesn't say "I am element 1 of the G array",
    it says "My name is 'G leftSquareBracket 1 rightSquareBracket'"
    much like name='bob' says "my name is 'b o b'"

    now to answer your question

    if you use
    <input type="checkbox" name="G[1]" value="AD">
    ...

    you could do something like this:

    Code:
    ...
    for( var i = 1; i < Gboxes; ++i ) {
      if( document.getElementById( "G[" + i + "]" ).checked == true ) {
         ++hits;
      }
    }
    ...
    You may find it easier to use:
    <input type="checkbox" name="G1" value="AD">
    and
    document.getElementById( "G" + i ).checked

    again, IE is nice enough to pick up the name as id, can't speak for other browsers

    document.getElementsByName( "G" + i )[0].checked
    will also work

    you could add ids to all of them like:
    <input type="checkbox" name="G1" id='chk' value="AD">
    <input type="checkbox" name="G2" id='chk' value="BD">
    ...

    and check them all at once with
    document.getElementsByName( "chk" )
    ...etc
    and their names would still be intact for later manipulation

  6. #6
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    don't feel bad, i had to try it before I figured out it didn't work :P
    would be nice if it did

  7. #7
    SitePoint Enthusiast casa's Avatar
    Join Date
    Oct 2002
    Location
    Alabama
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally figure out how to do what I wanted, even though I wasn't able to explain in very well. Here it is:
    Code:
    var match = 0;
    var i;
    for (i = 0; i < document.forms[0].elements.length; i++) { 
      var currentName = document.forms[0].elements[i].name; 
      if (document.forms[0].elements[i].type == "checkbox") { 
    	if (document.forms[0].elements[currentName].checked == true) {
    	  match++;
    	}
      }
    }
    if (match == 0) {
      alert ("Please check an DEPARTMENT or GROUP.");
      return false;
    }


Bookmarks

Posting Permissions

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