Link to home
Start Free TrialLog in
Avatar of intellisource
intellisourceFlag for South Africa

asked on

dynamic, database driven survey form validation

hi there, the two test forms are visible on the first dynamic testing formfor test survey one, and the second dynamic testing form for test survey two.
the forms may have either radio options, radio options with input text, checkboxes, checkboxes with text or supplier listings within groups that are unique per question.
Now  i am struggling to see how i should go about validating any one of these forms with the same3 script - to allow checking wether at least one option is selected within each group, checkboxes do not matter (are optional) and if a radio or checkbox is selected which has a related text input field, then it should have a valoue not equal to "". this is the script sofar, but i believe i got lost somewhere between the approach and the implementation of the dynamic form validation script:
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four','five','six','seven','eight','nine'];
var tn = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function toWords(s){
	s = s.toString();
	s = s.replace(/[\, ]/g,'');
	if (s != parseFloat(s)) return 'not a number';
	var x = s.indexOf('.');
	if (x == -1) x = s.length;
	if (x > 15) return 'too big';
	var n = s.split('');
	var str = '';
	var sk = 0;
	for (var i=0; i < x; i++) {
		if ((x-i)%3==2) {
			if (n[i] == '1') {
				str += tn[Number(n[i+1])] + ' ';
				i++;
				sk=1;
			} else if (n[i]!=0) {
				str += tw[n[i]-2] + ' ';
				sk=1;
			}
		} else if (n[i]!=0) {
			str += dg[n[i]] +' ';
			if ((x-i)%3==0) str += 'hundred ';
			sk=1;
		}
		if ((x-i)%3==1) {
			if (sk) str += th[(x-i-1)/3] + ' ';
			sk=0;
		}
	}
	if (x != s.length) {
		var y = s.length;
		str += 'point ';
		for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';
	}
	return str.replace(/\s+/g,' ');
}
function valform(form) {
	for (var i = 0; i < form.elements.length; i++) {
		var sel = false;
		var txt = "";
		var nam = new Array();
		var elm = form.elements[i];
		nam[0] = elm.name;
		if (nam[0] != nam[1] && elm.type == "radio") {
			if (sel == false) {
				var q = toWords(nam[0].split("_")[1]);
				alert("Radio group "+q+"needs at least one selection.");
				form[nam[0]][0].focus();
				return false;
			}
			sel = false;
			txt = "";
			nam[1] = nam[0];
			if (elm.selected) {
				sel = true;
				if (form["group_"+elm.value]) {
					txt = form["group_"+elm.value].value;
					if (txt == "" && elm.selected) {
						var q = toWords(nam[0].split("_")[1]);
						alert("Please provide text for the selected option "+q+".");
						form["group_"+elm.value].focus();
						return false;
					}
				}
			}
		}
		var val = elm.value;
	}
	return false;	// return false while testing validation
}

Open in new window

somebody please help!
thanks,
intellisource..
ASKER CERTIFIED SOLUTION
Avatar of intellisource
intellisource
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of intellisource

ASKER

no suggestions were made, so i continued working on itafter resolving other errors - and clarity eventually came to me.