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 

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.


Share the Post: