[thelist] Javascript form validation

Roee Rubin webdesign1 at irubin.com
Wed Oct 31 00:30:59 CST 2001


Hello,

I have been using a very simple script to perform client side javascript
validation. I am looking for something more generic.

Basically - I have two types of fields, text and select boxes. I am
interested in specifying the field name that I will like to validate at the
top of the function and the code will determine the type of field it is and
perform the validation (which for now could be just validating if data
exists or not).

Below is a snippet of the code I currently use, which is nothing to brag
about. Any help is appreciated.

<snip>

function validateform() {

	if
(document.mainform.Container.options[document.mainform.Container.options.sel
ectedIndex].value == "") {
		alert("Invalid field");
		document.mainform.Container.focus();
		return false;
	}

	if (document.mainform.Qty.value == "") {
		alert("Invalid field");
		document.mainform.Qty.focus();
		return false;
	}

	if (document.mainform.Length.value == "") {
		alert("Invalid field");
		document.mainform.Length.focus();
		return false;
	}

	if (document.mainform.Width.value == "") {
		alert("Invalid field");
		document.mainform.Width.focus();
		return false;
	}

	// ... and so forth

	return true;
}
</snip>



Roee Rubin
Irubin Consulting
roee at irubin.com
http://www.irubin.com/






More information about the thelist mailing list