• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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..
0
intellisource
Asked:
intellisource
  • 2
1 Solution
 
intellisourceAuthor Commented:
this issue has been resolved. the following script validates any radio groups, and any text boxes bound to a selected option.
function valform(form) {
	for (var i = 0; i < form.elements.length; i++) {
		var elm = form.elements[i];
		var type = elm.type;
		var name = elm.name;
		var val = elm.value;
		var sele = false;
		//alert(name);
		switch (type) {
			case "radio":
				var q = parseInt(name.split("_")[1]);
				for (var n = 0; n < form[name].length; n++) {
					if (form[name][n].checked) {
						sele = true;
					}
				}
				if (!sele) {
					alert("An answer is required for question "+q+").");
					form.elements[i].focus();
					return false;
				}
				break;
			case "text":
				if (form.elements[i-1].checked && elm.value == "") {
					alert("Input text is required for checked or selected options.");
					form.elements[i].focus();
					return false;
				}
				break;
		}
	}
	return true;
}

Open in new window

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now