5 Replies - 1089 Views - Last Post: 28 November 2008 - 10:57 AM

#1 sushama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-November 08

checkbox validation

Posted 26 November 2008 - 03:27 AM

Hi,
Here is the code for checkbox validation
I think there is a problem with 'return' statements.
As validation for textbox works fine but for checkbox it won't.
After clicking all checkboxes it shows alert message alert(Select user)....Please help me...
Thank you :)

function Vailidate(form)
{
'validate textbox
var total = 0;
if (form.taskdescr.value == '')
{
alert('Task Description is required.');
return (false);
}
'validate chkbox
var node_list = document.getElementsByTagName('input');
var chkboxes = [];
for (var i = 0; i < node_list.length; i++)
{
var node = node_list[i];
if (node.getAttribute('type') == 'checkbox')
{
chkboxes.push(node);
}
}
if (!chkboxes.checked)
{
alert('select user');
return false;
}
else
{
alert('selected');
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: checkbox validation

#2 xerxes333  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: checkbox validation

Posted 26 November 2008 - 07:08 AM

For future refrence
:code:

Try this . . .
function Vailidate(form){
	//validate textbox
	var total = 0;
	if (form.taskdescr.value == ''){
		alert('Task Description is required.');
		return (false);
	}
	//validate chkbox
	var node_list = document.getElementsByTagName('input');
	var allChecked = 0;
	for (var i = 0; i < node_list.length; i++){
		var node = node_list[i];
		if (node.getAttribute('type') == 'checkbox'){
			if(!node_list[i].checked)
				return(false);
		}
	}
} 
	

Was This Post Helpful? 0
  • +
  • -

#3 sushama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-November 08

Re: checkbox validation

Posted 27 November 2008 - 12:35 AM

Hi xerxes333 :D ,
I appreciate your quick response and help. thankx
But this code is not working means it is not taking any action on clicking 'submit' button.....



View Postxerxes333, on 26 Nov, 2008 - 06:08 AM, said:

For future refrence
:code:

Try this . . .
function Vailidate(form){
	//validate textbox
	var total = 0;
	if (form.taskdescr.value == ''){
		alert('Task Description is required.');
		return (false);
	}
	//validate chkbox
	var node_list = document.getElementsByTagName('input');
	var allChecked = 0;
	for (var i = 0; i < node_list.length; i++){
		var node = node_list[i];
		if (node.getAttribute('type') == 'checkbox'){
			if(!node_list[i].checked)
				return(false);
		}
	}
} 
	

Was This Post Helpful? 0
  • +
  • -

#4 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 115
  • View blog
  • Posts: 748
  • Joined: 25-October 08

Re: checkbox validation

Posted 27 November 2008 - 07:36 AM

Quote

But this code is not working means it is not taking any action on clicking 'submit' button.....

Post a link to the rest of your code as the error may be elsewhere.
BTW, have you looked at the error console in FF.
What browser are you using?
Was This Post Helpful? 0
  • +
  • -

#5 sushama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-November 08

Re: checkbox validation

Posted 28 November 2008 - 03:49 AM

View PostJMRKER, on 27 Nov, 2008 - 06:36 AM, said:

Quote

But this code is not working means it is not taking any action on clicking 'submit' button.....

Post a link to the rest of your code as the error may be elsewhere.
BTW, have you looked at the error console in FF.
What browser are you using?


Hi,
I have tested the code sent by xerxes333 on FF,IE+7 and Opera also, but it's not working.



Was This Post Helpful? 0
  • +
  • -

#6 xerxes333  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 504
  • Joined: 05-July 07

Re: checkbox validation

Posted 28 November 2008 - 10:57 AM

The code works just fine if you call it properly. In your FORM tag you need to add this attribute
onsubmit="return Validate(document.getElementById('myForm'))"
obviously substituting the document.getElementById('myForm') for your form node.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1