RSS Forums RSS
Please support our HTML and CSS advertiser: Lunarpages Web Hosting
Views: 10758 | Replies: 9 | Thread Tools  Display Modes
Reply
Join Date: Jul 2005
Posts: 41
Reputation: ohgosh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ohgosh's Avatar
ohgosh ohgosh is offline Offline
Light Poster

Help how to validate checkbox with different names?

  #1  
Aug 31st, 2005
HI~

i have a series of checkbox with different names, how do i validate them? validate to check that one of the checkbox must be checked in order to save the changes in the current page.

here are my checkbox codings
<input type="checkbox" name="stud" value="yes">Student
<input type="checkbox" name="lect" value="yes">Lecturer
<input type="checkbox" name="principal" value="yes">Principal 
<input type="checkbox" name="admin" value="yes">Administrator 
<input type="checkbox" name="parent" value="yes">Parent
thanks in advance..
cheers,
ohgosh
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: how to validate checkbox with different names?

  #2  
Aug 31st, 2005
If they are mutually exclusive, then give them all the same name. Otherwise, you'll just have to look at the "checked" property of each checkbox.
Reply With Quote  
Join Date: Jul 2005
Posts: 41
Reputation: ohgosh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ohgosh's Avatar
ohgosh ohgosh is offline Offline
Light Poster

Re: how to validate checkbox with different names?

  #3  
Sep 1st, 2005
Originally Posted by tgreer
Otherwise, you'll just have to look at the "checked" property of each checkbox.
what do u mean by this? i dun quite understand? i cant help giving them the same name.. :-| so i was wondering whether can i still validate them.
cheers,
ohgosh
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: how to validate checkbox with different names?

  #4  
Sep 1st, 2005
I mean, if the user must pick one, but only one, of the checkboxes, then you should give them all the same name.

If you must give them a separate name, then you will have to inspect the ".checked" property of each checkbox in your validation function.

Do you know how to look at the ".checked" property of a checkbox?
Reply With Quote  
Join Date: Jun 2005
Location: Cambridge, MA
Posts: 1,374
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 47
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: how to validate checkbox with different names?

  #5  
Sep 1st, 2005
You need to use server side code. Whatever processes the form should check for this.

Using Javascript can save the user some time, so that's good, too, but the code on the server that processes the form shouldn't assume that it has valid input.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: how to validate checkbox with different names?

  #6  
Sep 1st, 2005
Yes, data should be validated client-side, server-side, and also database-side.
Reply With Quote  
Join Date: Jul 2005
Posts: 41
Reputation: ohgosh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ohgosh's Avatar
ohgosh ohgosh is offline Offline
Light Poster

Re: how to validate checkbox with different names?

  #7  
Sep 2nd, 2005
Originally Posted by tgreer
Do you know how to look at the ".checked" property of a checkbox?
frankly speaking, i dont know how.. any guidance or sample code?

do u all mean that besides validating at the client-side by using js, i will also need to validate at the server-side? but how to?
cheers,
ohgosh
Reply With Quote  
Join Date: Jul 2005
Posts: 41
Reputation: ohgosh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ohgosh's Avatar
ohgosh ohgosh is offline Offline
Light Poster

Re: how to validate checkbox with different names?

  #8  
Sep 2nd, 2005
btw.. i have another qn..

can i retrieve the break tag from querystring? i have a textarea which i store in a querystring. the textarea will not show the break tag if i have break tag among the text i typed.

let me explain in greater details. i have a dropdownlist whereby onclick it will store the values from other html inputs' values (textarea, dropdownlist) into the querystring, and the html inputs' values should remain. the problem is that if i have a chunk of words in a textarea where there are paragraph breaks and when the dropdownlist onclick triggers, there are no paragraph breaks, the words are all joined together.

here are my codes:
<textarea name="txt" id="txt" rows="5" cols="100" class="text" mandatory='true' display='txt' pattern='ALPHA_NUMERIC_PLUS'><%=request.QueryString("txt")%></textarea>

function check(){
	document.form.action = "page.asp?txt="+document.form.txt.value;
	document.form.submit();
}
cheers,
ohgosh
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: how to validate checkbox with different names?

  #9  
Sep 2nd, 2005
Please post separate questions in a separate thread. Also, please don't use "chat shorthand" in forum messages. Rather, fully type out all words, and use proper spelling and punctuation. When seeking clarity, be clear!

Tell me exactly what it is about the checkbox situation that you want to "validate". First, are the checkboxes mutually exclusive? That is, are they part of a group of choices, and the user must pick one and only one of them?
Reply With Quote  
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the HTML and CSS Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:04 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