From the May 2008 Issue of Prestwood eMag |
| JavaScript Coding Tasks: Checking if a field is blank | |
|
Posted 21 years ago on 3/20/2003 and updated 11/7/2008
Take Away: How to validate form field is blank or not using JavaScript.
KB100191
|
// -->
[[[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:
onclick="CheckFieldBlank();">
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();">
0 Comments.
Share a thought or comment...
KB Article #100191 Counter |
6714 |
Since 4/2/2008
|