SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Udine - Italy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validate RADIObuttons

    How can I write this using getElementById in a xhtml page ?

    Code:
     for (i=0;i<2;i++)
          {
          (document.formname.gender[i].checked)?gselect=false:null
          }
       if (gselect)
          {
          check=true;
          alert("select your gender");
          }
    
    <input name="gender" type="radio" value="m" />male
    <input type="radio" name="gender" value="f" />female
    thank you

  2. #2
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script> function checkheorshe(){
    gselect=0
     for (i=1;i<3;i++) {
      gselect=(document.getElementById('gender'+i).checked)?gselect+1:gselect; 
      }
       if (gselect==0) { 
       // check=true; dunno what this one is for ? 
       alert("select your gender"); 
       return false; } 
       return true; } 
    </script> 
    </head> 
    <body> 
    <form name="heorshe" action="javascript:alert('ok I submit')" onsubmit="return checkheorshe();" > 
       <input name="gender" id="gender1" type="radio" value="m" /><label for="gender1">male</label> 
       <input type="radio" id="gender2" name="gender" value="f" /><label for="gender2">female</label> 
       <input type="submit" />
       <input type="reset" />
    </form> </body>
    </html>
    Last edited by SpaceFrog; Feb 18, 2005 at 10:41. Reason: gselect should not be a boolean

  3. #3
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a variant would be to have a global boolean initialized to false (var radiochecks=false) that you set to true on the onclick event of each radio button and reset to false on the onclik of the reste button
    a simple onsubmit="return radiochecks;" would do the job

    something like this:

    Code:
    <script> var radiochecks=false
    </script> 
    </head> 
    <body> 
    <form name="heorshe" action="javascript:alert('ok I submit')" onsubmit="if(!radiochecks){alert('select gender')};return radiochecks;" > 
       <input name="gender" id="gender1" type="radio" value="m" onclick="radiochecks=true"/><label for="gender1">male</label> 
       <input type="radio" id="gender2" name="gender" value="f" onclick="radiochecks=true"/><label for="gender2">female</label> 
       <input type="submit" />
         <input type="reset" onclick="radiochecks=false"/>
    </form>


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
  •