...

View Full Version : How do i test a text field?



SpiritualStorms
08-09-2004, 12:25 PM
I have the following function:


function testLauncher(){

var inputBox=document.Bear.babybear.value;

if(inputBox.value==undefined)
{
alert("You chose to not type anything");
inputBox.value="";
}
else
{
alert("You wrote, " + inputBox.value);
inputBox.value="";
}
}

The problem is, that no matter what i type in, i always end up getting the first alert message. How do i basically make it alert one of 2 potential conditions? At first i tried, if (inputBox=="") to make it alert the first alert, but that didnt work either. It seems like the undefined message was all that it would display for me.

Willy Duitt
08-09-2004, 01:09 PM
There is no such thing as: inputBox.value....
inputBox is a variable and you already declared its value....

var inputBox=document.Bear.babybear.value;

.....Willy

SpiritualStorms
08-09-2004, 01:23 PM
I have no idea what you mean.

Am i, or am i not to say:


if(inputBox.value==undefined)
?

i thought i need to access the value property to compare whats inside to something else?

What is wrong with this:


var inputBox=document.Bear.babybear.value;
?

neofibril
08-09-2004, 01:46 PM
What Willy said; i.e.,

var inputBox=document.Bear.babybear.value;

if(document.Bear.babybear.value.value==undefined)

jamescover
08-09-2004, 02:43 PM
<script>
<!--

function testLauncher(){

var inputBox = document.Bear.babybear;

if (inputBox.value == ""){
alert("You chose to not type anything");
} else {
alert("You wrote, " + inputBox.value);
inputBox.value = "";
}
}

//-->
</script>

<form name="Bear">
<input type="text" name="babybear" size="10" />
<input type="button" value="alert" onClick="javascript:testLauncher();" />
</form>





undefined
The value undefined.Core property


Implemented in
JavaScript 1.3

ECMA version
ECMA-262



Syntax
undefined
Description
undefined is a top-level property and is not associated with any object.

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.

You can use undefined to determine whether a variable has a value. In the following code, the variable x is not defined, and the if statement evaluates to true.

var x;
if (x == undefined) {
alert(x);
}

undefined is also a primitive value.


inputBox, any text field, returns an empty string like:

<input type="text" name="babybear" value="" size="10" />

Think of it like:

if (inputBox.value == ""){
alert("You chose to not type anything");
inputBox.value = "";

you wouldn't write:

if (inputBox.value == ""){
alert("You chose to not type anything");
inputBox.value = undefined;

Try this:


<script>
<!--

function testLauncher(){

var inputBox = document.Bear.babybear;

if (inputBox.value == ""){
alert(typeof(inputBox.value));
alert("You chose to not type anything");
} else {
alert("You wrote, " + inputBox.value);
inputBox.value = "";
}
}

//-->
</script>

SpiritualStorms
08-10-2004, 06:08 AM
OK, i think now i understand what was meant:


What Willy said; i.e.,

var inputBox=document.Bear.babybear.value;

if(document.Bear.babybear.value.value==undefined)


So when i declare a variable, and assign to it a specific object for its meaning, i shouldnt include within its assignment a specific property of that object/tag? Like you said above, if i include value at the end of babybear, then when i want to access that value property, i would have to do more or less a repeat? Like this:


if(document.Bear.babybear.value.value==undefined)
?

So i should always bear in mind the difference between an object as an assignment, and the accessing of a property through a variable?

jamescover
08-10-2004, 05:08 PM
if(document.Bear.babybear.value.value==undefined)

No. I think what he meant was what you were doing was the equivalent of the above.


inputBox is a variable and you already declared its value....

var inputBox=document.Bear.babybear.value;


So, you would simply say:


if(inputBox== "")

Not only is value.value redundant, but improper syntax.



-james



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum