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

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.

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.

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?

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.

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

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?

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();
}

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?

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)

//Below the code is an example for select checkbox with using different names.

<html>
<head>
<script>
function selectCheckBox()
{
if(document.getElementById('id11').checked==true)
{
document.frm.id2.checked=true
document.frm.id3.checked=true
document.frm.id4.checked=true
}
if(document.getElementById('id11').checked==false)
{
document.frm.id2.checked=false
document.frm.id3.checked=false
document.frm.id4.checked=false
}
}
function selectCheckBox1()
{
if(document.getElementById('id12').checked==false)
{
document.frm.id1.checked=false
}
}
function selectCheckBox2()
{
if(document.getElementById('id13').checked==false)
{
document.frm.id1.checked=false
}
}
function selectCheckBox3()
{
if(document.getElementById('id14').checked==false)
{
document.frm.id1.checked=false
}
}
</script>

</head>
<body>
<form name="frm">
All :<input type="checkbox" id="id11" name="id1" value="1" onClick="selectCheckBox()"><br>
A. :<input type="checkbox" id="id12" name="id2" value="2" onClick="selectCheckBox1()"><br>
B. :<input type="checkbox" id="id13" name="id3" value="3" onClick="selectCheckBox2()"><br>
C. :<input type="checkbox" id="id14" name="id4" value="4" onClick="selectCheckBox3()"><br>
</form>
</body>
</html>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.