Page 1 of 1

Form Validation problem

Posted: Thu Jul 07, 2011 11:12 am
by IGGt
Hi, I have little experience in javascript, but need to create a form validation script.

I have a page that contains a set of options, some are checkboxes, some are radio buttons, and I need to guarantee at least one of each is selected. So far I have a list that contains radio buttons with value=group1 and some checkboxes with value = sl_$dbid (where $dbid is created in PHP and resolves to sl_1, sl_2, sl_3 etc.)

I need to confirm that the a radio button is selected, and at least one of the checkboxes is selected.

I have some javascript I put together, which doesn't yet work, but if anyone can offer some advice it would be appreciated:

Code: Select all

function ValidateDBSelect(){
//Radio Button
	u=''	
	for (a=0;a<document.forms[0].group1.length;a++) {
		if (document.forms[0].group1[a].checked) {
			user_input = document.forms[0].group1[a].value;
			u++
	}
}

if ((u.value==null)||(u.value==""||u.value<1)){
		alert("Please Select a Radio Button")	
		u.focus()
		return false
	}
	

//Check Box
	v=''
	for (b=0;b<document.forms[0].group1.length;b++) {
		if (document.forms[0].sl_b.checked) {
			user_input = document.forms[0].sl_b.value
			v++
		}
	}
	
if ((v.value==null)||(v.value==""||v.value<1)){
		alert("Please Select at least one Slave Database")	
		v.focus()
		return false
	}
}	

Re: Form Validation problem

Posted: Thu Jul 07, 2011 11:46 am
by social_experiment
viewtopic.php?f=13&t=129971
This topic contains some (basic) javascript i created to check that a certain amount of radio buttons were set, maybe you can modify the code to suit your purpose.
Hth