How to test an empty input box?

C

chirs

I try to test a field to see if it's empty. But it prints "It is not
null". Why and how can I fix it?

<html>
<head>
</head>
<body>
<input type="text" name="box1">

<script>
if (box1.value != null) {document.write("It is not null")}
else {document.write("empty")}
</script>
</body>
</html>
 
L

Laurent Bugnion, GalaSoft

Hi,
I try to test a field to see if it's empty. But it prints "It is not
null". Why and how can I fix it?

<html>
<head>
</head>
<body>
<input type="text" name="box1">

<script>
if (box1.value != null) {document.write("It is not null")}
else {document.write("empty")}
</script>
</body>
</html>

null is the value that an object has when it hasn't been initialized, in
other word when no memory has been allocated for it yet.

The "value" property exists, however, even if it is empty. To test if a
field is empty, you must compare it to an empty string:

if ( document.formName.fieldName.value == "" )
{
//empty
}

Laurent
 
J

Jerry Park

Hi,



null is the value that an object has when it hasn't been initialized, in
other word when no memory has been allocated for it yet.

The "value" property exists, however, even if it is empty. To test if a
field is empty, you must compare it to an empty string:

if ( document.formName.fieldName.value == "" )
{
//empty
}

Laurent
Alternately, you can compare the length of the object's value to 0 .
 
T

Thomas 'PointedEars' Lahn

null is the value that an object has when it hasn't been initialized,

No, that is `undefined', the primitive sole value of the Undefined type.
Try

var a;
alert(typeof a);

`null' is a special object literal, the primitive sole value of the
Null type. It "represents the null, empty, or non-existent reference."
Try

alert(typeof null);

See
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/ident.html#1008306>

<http://www.ecma-international.org/publications/standards/Ecma-262.htm>
ECMAScript Edition 3 (on which JavaScript 1.5 is based upon), sections
4.3.9 to 4.3.12
in other word when no memory has been allocated for it yet.

There is *always* memory allocated for a variable/object/property,
otherwise it would not be accessible (at least memory for an entry
in the global hash table must be allocated). AFAIK this is
independent of the programming language used.


PointedEars
 
R

Richard Hockey

chirs said:
I try to test a field to see if it's empty. But it prints "It is not
null". Why and how can I fix it?

<html>
<head>
</head>
<body>
<input type="text" name="box1">

<script>
if (box1.value != null) {document.write("It is not null")}
else {document.write("empty")}

give this a try

<script type="text/javascript">
// build regular expression object to find empty string or any number of
spaces
var blankRE=/^\s*$/;

function CheckEmpty(TextObject)
{
if(blankRE.test(TextObject.value))
{
alert('box1 is empty or contains only spaces.');
TextObject.focus();
}
}
</script>

and in the form
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top