|
|
Home » Javascript » Article
Javascript Form Validate Empty Field function
|
| Article by: | Jeff Anderson ( 1362 ) (2/26/2002) |
|
| Summary: | This javascript function allows you to check whether a form field has been completed or not. |
|
| Viewed: 402300 times |
Rating (49 votes): |
|
3.6 out of 5 |
|
|
|
This function can be used to check whether the user has entered anything in a given field.
There are two possibile types of 'emptiness' a form can return - a zero length string, or a null value.
Here we simply check both these options, and return true if either of these are found to be true.
function IsEmpty(aTextField) {
if ((aTextField.value.length==0) ||
(aTextField.value==null)) {
return true;
}
else { return false; }
}
| |
|
To check the particular field, you need to pass it in, in the form document.formname.fieldname
It's important to remember to give your form a name in the <form> tag which is referenced here.
So, for example, if your form name is form1, and the field name is 'address1' you call the IsEmpty function as follows :
To understand more about how to use the IsEmpty function to validate a form, see Form Validation function
|
|
View highlighted Comments
User Comments on 'Javascript Form Validate Empty Field function'
|
Posted by :
Archive Import (Zoya) at 02:15 on Monday, September 09, 2002
|
This function will interpret a string of spaces as a filled out field, although it is considered empty in most practical cases. Also, value of a text field is never Null, but rather a zero-length string. I think this code would work better:
function isEmpty(aTextField){
var re = /s/g; //Match any white space including space, tab, form-feed, etc.
var str = aTextField.replace(re, "");
if (str.length == 0) {
return true;
} else {return false;}
}
| |
Posted by :
Archive Import (Zoya) at 03:22 on Monday, September 09, 2002
|
Correction for the previous post:
function isEmpty(aTextField){
var re = /\s/g; //Match any white space including space, tab, form-feed, etc.
var str = aTextField.replace(re, "");
if (str.length == 0) {
return true;
} else {return false;}
}
| |
Posted by :
Archive Import (Ghassan Beydoun) at 06:03 on Tuesday, October 15, 2002
|
Correction for the previous post (IE support):
function isEmpty(aTextField){
var re = /\s/g; //Match any white space including space, tab, form-feed, etc.
RegExp.multiline = true; // IE support
var str = aTextField.replace(re, "");
if (str.length == 0) {
return true;
} else {return false;}
}
| |
Posted by :
Archive Import (Kay) at 18:31 on Saturday, November 09, 2002
|
The following code didn't work for me:
function isEmpty(aTextField){
var re = /\s/g; //Match any white space including space, tab, form-feed, etc.
RegExp.multiline = true; // IE support
var str = aTextField.replace(re, "");
if (str.length == 0) {
return true;
} else {return false;}
}
I received a message that the property or method wasn't supported on the following line:
var str = aTextField.replace(re, "");
Does anyone have any ideas or have they gotten this code to work?
I was able to the following code to work but it allows spaces as being true and that won't work for what I need.
Thanks in advance,
Kay
| |
Posted by :
Archive Import (Kay) at 18:33 on Saturday, November 09, 2002
|
Sorry. I forgot to add the code that did work. Here it is:
function IsEmpty(aTextField) {
if ((aTextField.value.length==0) ||
(aTextField.value==null)) {
return true;
}
else { return false; }
}
Thanks,
Kay
| |
|
|
|
|
|
|
Posted by :
Archive Import (waleed wahba) at 07:59 on Wednesday, December 11, 2002
|
if(document.form.text1.value=="")
{
alert("Invaled");
return false;
}
| |
|
|
Posted by :
Archive Import (Terry) at 22:44 on Wednesday, June 11, 2003
|
I came across this code and I am novice at javascript. Is there a way to determine if the form field has not been completed. Then, automatically input a default value for the non-completed form field? Then, it will process based on default values. Thanks for any ideas.
Terry
| |
Posted by :
Archive Import (Abe) at 08:14 on Friday, June 13, 2003
|
There is a way to set default values. If the field is not completed set document.nameofform.nameoffield.value to whatever you want the field to be.
Obviously, you'll need to replace "nameofform" and "nameoffield" to whatever their names are in your HTML code.
| |
Posted by :
Archive Import (dude) at 17:02 on Sunday, July 20, 2003
|
To Zoya,
Your Code works perfect! It is a good code for checking Empty and any white spaces in the form fields.
Thank you for that!
Regards,
a dude
| |
Posted by :
Archive Import (Judy) at 19:09 on Tuesday, August 05, 2003
|
Will this function work in reverse? I have 4 text fields and require only one to be completed. Only if a field is completed should it then be validated. I have tried the following with no success in IE. The first part works, but the second part it ignored.
function validate(){
if ((document.housecall.email.value == "") && (document.housecall.phone.value == "") && (document.housecall.cell.value == "") && (document.housecall.pager.value == ""))
{
alert('Please provide your contact information');
return false;
}
else if ((document.housecall.email.value.length!=0) || (document.housecall.email.value!=null) || (document.housecall.email.value!= ""))
{
alert('Validate email');
return false;
}
}
| |
|
|
Posted by :
Archive Import (Judy) at 16:40 on Saturday, August 09, 2003
|
I'm not sure what you mean. Perhaps viewing the whole page will help...
http://www.glass-dr.com/form_test.htm
| |
Posted by :
Archive Import (Jr k kurien ) at 02:33 on Wednesday, September 10, 2003
|
I would like to have a validation code for trimming white spaces in the beginning as well as ending of a string.
| |
Posted by :
Archive Import (Jr k kurien ) at 02:37 on Wednesday, September 10, 2003
|
I would like to have a validation code for trimming white spaces in the beginning as well as ending of a string.Expecting a quick response.
| |
Posted by :
bethchebaygolly at 22:59 on Sunday, December 12, 2004
|
I got a problem with trimming my string...i have a field which requires an input. when the user tries to input a space it still updates the data. what will i do?
| |
Posted by :
xman at 18:41 on Thursday, June 09, 2005
|
Actually, here is a better function without any workarounds for crappy browsers like IE:
function isEmpty(mytext) {
var re = /^\s{1,}$/g; //match any white space including space, tab, form-feed, etc.
if ((mytext.value.length==0) || (mytext.value==null) || ((mytext.value.search(re)) > -1)) {
return true;
}
else {
return false;
}
}
| |
Posted by :
artmedia at 06:50 on Friday, April 07, 2006
|
in somecases aTextField.value==null don't work, better use aTextField.value==""
see a sample in http//www.comucom.com
| |
Posted by :
solostaran at 03:05 on Thursday, June 01, 2006
|
What's bad about:
function IsEmpty(Txt) {
return ((Txt.value.length==0) || (Txt.value==null))
}
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
Javascript - Enable and Disable form elements by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form. |
 |
Check IsNumeric Function by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric. |
 |
Javascript Onload Event by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy |
 |
Form Validation Function by Jeff Anderson
A javascript validation function that you can use to validate all types of forms. |
 |
JavaScript Field Is Empty Form Validation by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not. |
 |
Javascript Get Selected Text by Jeff Anderson
A cross-browser script to get text selected by the user |
 |
Check Email Validation Function by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form. |
 |
Multiple submit buttons on a single form by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button. |
 |
Validate Form and Disable Submit Button by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating. |
 |
Javascript onUnload Event Handler by Jeff Anderson
The onUnload Event Handler allows you to perform an action as the user leaves the page. |
 |
| |