It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming HTML Forum

validate checkbox

validate checkbox

Postby hazrizal84 on Sun Dec 28, 2008 2:59 pm

hi..i just wanna ask..
in my form, there are several arrays of checkbox..and a button named 'delete'. when the delete button pressed, onSubmit will check if there is any checkbox ticked..if no box been ticked, alert message will tell the user that they must click any of the checkbox to proceed to the delete part..so, how i gonna solve this prob?..thanx 4 ur help..
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Re: validate checkbox

Postby rangana on Mon Dec 29, 2008 3:32 am

Here's a basic example:
Code: Select all
<script type="text/javascript">
var ray=
{
validate:function(el)
   {
   var chckArr = el.getElementsByTagName('input'),check=false;
   for(var i = 0; i < chckArr.length; i++ )
      if(chckArr[i].getAttribute('type')=='checkbox')
         check=chckArr[i].checked?true:false;

   if(!check)
      {
      alert('You must click any of the checkbox to proceed to the delete part.');
      return false;
      }
   }
}
</script>
<form id="myform" onsubmit="return ray.validate(this)">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="submit" value="delete">
</form>
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: validate checkbox

Postby hazrizal84 on Mon Dec 29, 2008 4:01 am

thanx alot 4 ur help..i really appreciate it..but there is a small problem here..the script is valid only if the last checkbox is ticked..let's say i have 10 checkboxes, if i don't click any of those boxes, the alert message will pop up..it's good..but, if i click all the boxes excluding the last box, the alert message also popped up. can u fix this prob for me?..thanx again..
hazrizal84
 
Posts: 22
Joined: Tue Jun 10, 2008 11:25 am

Re: validate checkbox

Postby rangana on Mon Dec 29, 2008 4:27 am

Just replace this part:
Code: Select all
check=chckArr[i].checked?true:false;


...with:
Code: Select all
check=chckArr[i].checked?true:check;
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: validate checkbox

Postby Akidnameddroo on Mon Mar 28, 2011 7:19 am

forgive me I'm a bit of a rookie
so I would like to use the same code, except I want the button at the end to be a link.



so I just replaced the last line of

<input type="submit" value="A link for megan">

With

<FORM METHOD="LINK" ACTION="the url">
<input type="submit" value="Tester">
</FORM>

when I use this by itself it works fine but with your code it dosent work. :(
can't figure it out...
Akidnameddroo
 
Posts: 1
Joined: Mon Mar 28, 2011 7:14 am


Who is online

Users browsing this forum: No registered users and 6 guests