Form Validation problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Form Validation problem

Post 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
	}
}	
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Form Validation problem

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply