RSS Forums RSS
Please support our HTML and CSS advertiser: Lunarpages Web Hosting

how to validate checkbox with different names?

Join Date: Aug 2005
Posts: 20
Reputation: cppforlife is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
cppforlife cppforlife is offline Offline
Newbie Poster

Solution Re: how to validate checkbox with different names?

  #10  
Sep 2nd, 2005
I think you mean mean it this way:
Before we process input from form we should validate it by using javascript. To validate checkboxes or any other elements of the form you should put return statement in the onSubmit statement>>>


 <script language=javascript>
  function validate(){
   var email=document.forms[0].email.value;
   var comment=document.forms[0].comment.value;
   if(email==""||(email.indexOf('@')==-1)||(email.indexOf('.')==-1)){
    alert("E-mail field is invalid!");
    document.forms[0].email.focus();
    return false;
   }
   if(comment==""){
    alert("Comment field is empty!");
    document.forms[0].comment.focus();
    return false;
   }
   return true;
  }
 </script>

<form name="contact_form" method="post" action="http://..." onSubmit="return validate()">

<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>

validate() returns boolean value that tells the form to post input or not.
This was basics validation

To validate checkboxes you need to check value(checked or not)

 <script language=javascript>
  function validate(){
       var email=document.forms[0].email;
       if(email.checked!=true){
          alert("Email is not checked!");
          return false;
       }
       return true;
  }
 </script>

----------------------------------
PS: you can use model document.formname.objectname.checked instead of document.forms[0].email but some netscape browsers will generate error

PS: document.forms[number].email- number is a number of the form on the page (starts form 0)
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:23 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC