954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to validate checkbox with different names?

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..

ohgosh
Light Poster
41 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 
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.

ohgosh
Light Poster
41 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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?

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Yes, data should be validated client-side, server-side, and also database-side.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 
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?

ohgosh
Light Poster
41 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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();
}
ohgosh
Light Poster
41 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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?

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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()">


<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 modeldocument.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)

cppforlife
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 



All :

A. :

B. :

C. :

jyotishankar
Newbie Poster
2 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You