Description

Loops through all form fields and ensures that they are not blank and do not only contain blank spaces.

Demonstration

Name:
Email:
Code is called in an onsubmit handler in the form (ie. <form name="yourform" onsubmit="return validate(this);">)

Code

<script language="javascript" type="text/javascript">

function validate(frm) {
	msg = "Please fill out the following fields:\n";
	x = 0;
	for (i=0; i<frm.elements.length; i++) {
		if(/^\s*$/.test(frm.elements[i].value)) {
			msg += frm.elements[i].name+"\n";
			x = 1;
		}
	}
	if (x == 1) {
		alert (msg);
		return false;
	}
}
</script>

Comments:

Tested in: IE6, Opera 7, NN4.7, NN7, and Mozilla 1.2.
Works in: IE6, Opera 7, NN4.7, NN7 and Mozilla 1.2.
Doesn't work in: N/A

Copyright© 2003, Infinity Web Design