I.T. Discussion Community!
-Collapse +Expand
Search JavaScript Group:

-Collapse +Expand JavaScript Store

Prestwood eMagazine

June Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...JavaScript a...Coding Tasks   Print This     
  From the May 2008 Issue of Prestwood eMag
JavaScript Coding Tasks:
Checking if a field is blank
Posted 17 years ago on 3/20/2003 and updated 11/7/2008
Take Away: How to validate form field is blank or not using JavaScript.


// -->

[[[Article formattng messed, we will fix ASAP. Sorry for the inconvenience.]]]

For this example we'll use the document object model to check a checkbox.

Key Points

  • Name the form (in this first example, we named the form Example1. You do this so you can use dot notation to refer to "theForm.theField.value".
  • Text objects have value property you can use to check.
  • You can add an onClick JavaScript event to the form. In this case, we used the event to call a function.

Here's the function code :

function CheckFieldBlank() {
if (Example1.Field1.value == ""){
alert("It's Blank!");
} else { alert("It's not blank.");

Here's the form code:




Checking if a field has a specific value

One more example. This example show how to check for a specific value. In this case we  check for the value 'Mike' in the field.

Here's the function:

function CheckFieldMike() {
if (Example2.Field1.value == "Mike"){
alert("It's Mike!");
} else { alert("It's not Mike.");

Here's the form code:



value="Check If Blank" onclick="CheckFieldMike();">


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P134A1
Enter key:
KB Post Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #100191 Counter
Since 4/2/2008

Follow PrestwoodBoards on: 

©1995-2020 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech