Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if a form box is empty

Status
Not open for further replies.

jay25

MIS
Jun 17, 2002
16
US
I am trying to get some statements executed in a case a form box is empty and I tried comparing the value (theform.thebox.value) to null, NaN, undefined but it doesn't seem to work. Anybody know how to do it?
Code:
if (theform.thebox.value == null)
alert("You got it!");
else
alert(theform.thebox.value);
 
Hey Jay25,

When you say form BOX do you mean text input?

if so this should work:

if (document.form.field.value == "")
{
alert("It's empty");
}

If you mean a checkbox input then use this:

if (document.test.testbox.checked == true)
{
alert("checked");
}

Hope that covers it...
If not post more code, we'll figure it out.

 
<html>
<head>
<script type=&quot;text/javascript&quot;>
<!--
function test() {
if (document.theform.thebox.value == &quot;&quot;) {
alert(&quot;no value&quot;);
} else {
alert(document.theform.thebox.value);
}
}
//-->
</script>
</head>
<body>
<form name=&quot;theform&quot;>
<input type=&quot;text&quot; name=&quot;thebox&quot;><br>
<input type=&quot;button&quot; value=&quot;test&quot; onclick=&quot;test()&quot;>
</form>
</body>
</html> ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
seems we posted at the same time ... ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
It was a text input box and the empty quotes worked. Thanks! For some reason, everytime I got back to this site, I had 0 responses. But when I clicked on the post, I saw that you all wrote in it. And I didn't use the back button to get back to the site but used the favorites. I had to reload my browser to get an updated front page. Maybe it's just my computer.
 
jay25,

if you're using IE, check in Tools > Internet Options > Settings (Temporary Internet Files) and make sure you're not set to &quot;Never&quot;. I'm set to &quot;Automatically.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top