[JS] Checking for empty fields

Associate
Joined
21 May 2003
Posts
1,365
I'm trying to check all text inputs in a form to make sure they're not empty. Any ideas why this code isn't working?

Code:
	<script type="text/javascript">
		function IsEmpty(aTextField) {
			if ((aTextField.value.length == 0) || (aTextField.value == null)) {
				return true;
			} else {
				return false;
			}
		}
	
		function checkForm() {
			var theForm = document.getElementById('myForm');
			
			for (i=0; i < theForm.elements.length; i++) {
				var element = theForm.elements[i];
				
				if (isEmpty(element)) {
					alert("You must supply a name for every person");
					return false;
				}
			}
			return true;
		}
	</script>

...snip...

<form name="myForm" id="myForm" method="post" action="nextPage.php" onsubmit="checkForm()">
 
Back
Top Bottom