devxlogo

Checking for Blank Form Fields

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:

 

The function formCheck() should go in the 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.

See also  Small Business Strategies with Venmo
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist