jquery - Validating Checkboxes in javascript -


hi new javascript , don't know how validate check-boxes have looked many examples still don't understand can please tell me how validate , drop down menu inst validating @ in advance appreciated.

my java script , html  function validateform(){      var fname, lname, sex, address, email, length, songs, a, i, check, error;      a=0;     check=false;     error=false;      fname=document.getelementbyid("firstname").value;     lname=document.getelementbyid("lastname").value;      sex=document.getelementsbyname("sex");      address=document.getelementbyid("address").value;     email=document.getelementbyid("email").value;     length=document.getelementbyid("len").value;  //  songs=document.getelementbyname("f_song");      if(fname=="" || fname==null){          alert("please input first name");         error=true;         return false;     }      if(lname=="" || lname==null){          alert("please input last name");         error=true;         return false;     }      for(i=0; i<sex.length; i++){          if(sex.item(i).checked==false){                  a++;         }     }      if(a==sex.length){              alert("please select male or female");             error=true;             return false;      }      if(address=="" || address==null){          alert("please input address thanks");         error=true;         return false;       }      if(email=="" || email==null){          alert("please enter email address");         error=true;         return false;     }      if(length=="" || length==null){          alert("be select how long have been fan");         error=true;         return false;     }           alert("am working ");  }   html   <head> <title>] </title> <link rel="stylesheet" type="text/css" href="wit_frm.css"/>  <script type="text/javascript" src="java.js">      </script> </head>  <body>      <div id="container">          <div id="header">          </div>          <div id="topnav">              <div id="link">                 <a href="wit.html">home</a>                 <a href="http://www.whitneyhouston.com">fan site</a>                 <a href="wit_form.html">register</a>             </div>          </div>            <div id="contentarea">              <form method="post" action="" onsubmit= "return validateform();">              <fieldset>             <legend>personal information</legend>              first name:<input type="text" name="fname" id="firstname"/>             <br/>             <br/> last name:<input type="text" name="lname" id="lastname"/>             <br/>             <br/> sex:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <input type="radio" name="sex" id="m"/>male  <input type="radio" name="sex" id="f"/>female             <br/>             <br/>address:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<textarea id="address" name="add" rows="4" cols="30"></textarea>              </fieldset>              <fieldset>             <legend> user information</legend>             <br/>email: &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp; <input type="text" name="e_address" id="email"/>             <br/>             <br/>no of years listening  music: <select name="n_years" id="len">                                                 <option value="less yr"> less 1yr</option>                                                 <option value="2-5 years">2-5 years</option>                                                 <option value="5+ years">5+ years</option>                                                 </select>             <br/>                                                <br/>favorite songs:i love you<input type="checkbox" name="f_song" value="i love you"/> you<input type="checkbox" name="f_song" value="i you"/></p>                               <br/> &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp;  &nbsp;  have nothing<input type="checkbox" name="f_song" value="i have nothing"/> 1 moment in time<input type="checkbox" name="f_song" value="one moment in time"/></p>                             <br/>&nbsp;  &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; wanna dance somebody<input type="checkbox" name="f_song" value="i wanna dance somebody"/> greatest love of all<input type="checkbox" name="f_song" value="greatest love of all"/></p>              </fieldset>             <br/><input type="submit" name="sub_button" value="submit"/> &nbsp;  &nbsp;  &nbsp; <input type="reset" name="res_button" value="clear"/>              </form>            </div>          <div id="footerfix">          </div>          <div id="footer">              <div id="link_1">                 <a href="wit.html">home</a>                 <a href="">fan site</a>                 <a href="">about her</a>             </div>         </div>      </div>     </body> </html> 

i this:

bin example

var f_song = document.getelementsbyname("f_song"); var onesongselected = 0; //loop through checkbox elements, , tally selected for(var ss = 0; ss < f_song.length; ss++){   if(f_song[ss].checked){     onesongselected++;   } } //if none selected, alert error, , return false if(onesongselected === 0){   alert("please select @ least 1 song");   return false; } 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -