Software, your way.
burger menu icon
WillMaster

WillMasterBlog > JavaScript

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Instant Required Field Check

When a form is submitted, the form processing software on the server may check that all required fields contain information. When any are missing, the for user returns to the form and supplies the required information.

JavaScript can do an instant check when the form is submitted and prevent the submission if required fields are empty, along with an appropriate message.

No need to reload the form. Less hassle for the user.

Note that this system does not check that the information is valid, only that information is present.

Each form field to be checked needs an id. An id is assigned with id="___", the underscore replaced with a valid id. A valid id —

  • starts with a letter,
  • is composed only of letters and numbers, and
  • is unique on the web page (no duplicate ids allowed on the page).

Example:

<input type="text" id="abc" name="address">

Make a list of the id values for each required field. You'll need them in this JavaScript:

<script type="text/javascript">
/* 
   Instant Required Field Check
   Version 1.0
   February 15, 2010

   Will Bontrager
   https://www.willmaster.com/
   Copyright 2010 Bontrager Connection, LLC

   Bontrager Connection, LLC grants you 
   a royalty free license to use or modify 
   this software provided this notice appears 
   on all copies. This software is provided 
   "AS IS," without a warranty of any kind.
*/
// Leave next line as is.
var ID_Message = new Array();

// // // // // // // // // // // //
// Configuration section:
//
// Below, list the id an error message for each required 
//    field in this format:
//       ID_Message["idvalue"] = "Error Message";
// Examples:
//       ID_Message["email"] = "Please provide email address.";
//       ID_Message["phone"] = "A telephone number is required.";
// If the message itself contains quotation marks, they 
//    need to be escaped with a preceding backslash 
//    character. Example:
//       ID_Message["nick"] = "Please provide your \"nick\" name.";

ID_Message["email"] = "Please provide email address.";
ID_Message["phone"] = "A telephone number is required.";
ID_Message["nick"] = "Please provide your \"nick\" name.";


// End of configuration section.
// // // // // // // // // // // //
function CheckAllRequiredFields() {
var message = new Array();
for(var k in ID_Message) {
   if(!document.getElementById(k).value.length) { message.push(ID_Message[k]); }
   }
if(message.length) {
   alert(message.join("\n\n"));
   return false;
   }
return true;
}
</script>

About 20 lines down, past the header and legalese section of the JavaScript, you'll see a configuration section.

In the configuration section, list each required field id value and the relevant error message to use if the field is empty. The JavaScript contains instructions on exactly how to list them.

The JavaScript can be put anywhere on the page, above or below the form, in the HEAD or BODY area.

Last, put an onsubmit="return CheckAllRequiredFields()" attribute into the form's form tag. Example:

<form onsubmit="return CheckAllRequiredFields()">

Here is a form using the above JavaScript exactly. (When all fields contain information and button is clicked, this page reloads.)

Email:
Telephone:
Nickname:

If interested in repeating the example, here is the form code.

<form onsubmit="return CheckAllRequiredFields()">
Email: <input type="text" name="email" size="20" id="email"><br>
Telephone: <input type="text" name="telephone" size="20" id="phone"><br>
Nickname: <input type="text" name="nickname" size="20" id="nick"><br>
<input type="submit">
</form>

To implement the system:

  1. Assign a valid id to each required field.

  2. Customize the JavaScript for each required field id.

  3. Put an onsubmit="return CheckAllRequiredFields()" attribute into the form's form tag.

You now have an instant required field check.

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

The HTML Q Tag

Create a style then use the HTML q tag to apply the style. This is about as easy as it gets for attracting attention to quotes.

Plain Text To HTML Converter

Here is a function to convert plain text from a form textarea field into HTML for publishing on a web page or in an email.

HTML for Meme Making

No fancy image creation software is needed to make memes. An easy way is to create a web page with text over an image or color background and screenshot it.

Image Color Saturation

The CSS filter:saturate() declaration can be used to lesson color saturation and to intensify color saturation of images.

PDF to Text Conversion

If you can use a PDF to text converter and your website is on a Linux machine, this one should do you well.

Text Emphasis

When your writing can be improved with the correct emphasis, this CSS may be used.

Dropdown Links

I used to see many dropdown links during the earlier part of my internet experience.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2025 Will Bontrager Software LLC