Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web
Expertise: Intermediate
Jan 29, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Checking for Blank Form Fields

If you use an HTML form at your Web site, you may not want anyone to submit the form if required fields are left blank. You can check for blank form fields with JavaScript on the client side and keep any forms from being submitted if they don't meet your requirements. What you need to do is attach a checking function to the Submit button of your form, like so:
<INPUT TYPE="submit" onClick="return formCheck()">
The function formCheck() should go in the <HEAD> section of the document, where myForm is the name of the form, myElement is the name of the element being checked, and 2 is the minimum number of characters allowed in the field:
function formCheck() {
	var not_blank = true;
	if (document.myForm.myElement.value.length < 2) {
		not_blank = false;
	return not_blank;
You can also expand the function to cover as many form elements as you'd like, and add a pop-up error message to explain to the user why the form was not submitted.
Steve Renaker
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date