471,338 Members | 1,020 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

textarea with value "" undefined?

Hello,

I have a form with an input field, a textarea, having a value of ""
Also, I have some javascript code, which may be reduced to the following:

function validate(mycode)
{
alert(mycode.value == undefined);
}

validate(myForm.field);

where 'field' is the field I am talking about, the one that looks like this:

<INPUT TYPE=text NAME=field VALUE="" MAXLENGTH=4 SIZE=4>

My alert shows up as true.

Can someone please explain why, or point me where to look?

thank you very much,

denis

Jul 23 '05 #1
2 3245
Denis Perelyubskiy wrote on 17 okt 2004 in comp.lang.javascript:
function validate(mycode)
{
alert(mycode.value == undefined);
}

validate(myForm.field);

where 'field' is the field I am talking about, the one that looks like
this:

<INPUT TYPE=text NAME=field VALUE="" MAXLENGTH=4 SIZE=4>


alert(myForm.field.value == "");
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
Denis Perelyubskiy wrote:
[snip]
Can someone please explain why, or point me where to look?


The alert will evaluate what is inside the brackets and put the result
in the alert box. Your test asks "is the value of the text box
undefined?" and the answer is no, or "false". I don't know why you are
getting true, you must have an error in your code somewhere. Here is a
working snippet:

<script type="text/javascript">
function validate(mycode) {
alert(mycode.value == undefined);
}
</script>

<form action="" name="myForm">
<INPUT TYPE="text" NAME="field" VALUE="" MAXLENGTH="4" SIZE="4">
<input type="button" value="click"
onclick="validate(myForm.field)">
</form>

Your test asks if the element "field" has a value, and it does - either
an empty string or whatever has been typed in, so the test is always
"true". If you want to see if the string has a value, then use
Evertjan's solution.

As written, the alert returns "true" if the field is empty, or "false"
if it has something in it. To reverse the sense, use:

alert(!myForm.field.value == "");

and you will get false if it's empty and true if it isn't.

However, I'd presume that you need to do far more validation than
simply test that something has been added.

Rob.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by HD | last post: by
14 posts views Thread by TTroy | last post: by
3 posts views Thread by Mark Sullivan | last post: by
21 posts views Thread by Steven T. Hatton | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.