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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if the form is empty

Status
Not open for further replies.

mrkyn

MIS
Jan 30, 2002
78
US
I want to verify if all fields of the form are filled up or not. Here is the function I use which I call in form using onsubmit (see below). It opens sell.asp without checking. Please help me find my error. Thank you.
<HTML>
<HEAD>
<script language=Javascript>
<!--
function Empty()
{
if ((document.frmItem.ItemName.value == null) || (document.frmItem.Description.value == null) || (document.frmItem.AskingPrice.value == null) || (document.frmItem.Place.value == null))
{
alert ("Please complete all fields");
return false;
}
else
{
if <%Session("success")%>
{
alert ("Please go to step 3 to add an image");
return false;
}
else
return true;
}
}
-->
</SCRIPT>

<FORM ACTION="sell.asp" NAME="frmItem" METHOD="POST" onSubmit="return Empty()" target="_blank">
 
try changing

if ((document.frmItem.ItemName.value == null) || (document.frmItem.Description.value == null) || (document.frmItem.AskingPrice.value == null) || (document.frmItem.Place.value == null))

to

if ((!document.frmItem.ItemName.value) || (!document.frmItem.Description.value) || (!document.frmItem.AskingPrice.value) || (!document.frmItem.Place.value))

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Your suggestion did not work. I think it should be changed to

if ((!document.frmItem.ItemName.value) && (!document.frmItem.Description.value) && (!document.frmItem.AskingPrice.value) && (!document.frmItem.Place.value))

However, I tried both. Both did not work. Thank you just the same.
 
try
Code:
<script language=Javascript>
<!--
  function Empty()
  {
    if ((document.frmItem.ItemName.value == "") || (document.frmItem.Description.value == "") || (document.frmItem.AskingPrice.value == "") || (document.frmItem.Place.value == ""))
    {
      alert ("Please complete all fields");
      return false;
    }
    else
    {
      if <%Session("success")%>
      {
          alert ("Please go to step 3 to add an image");
        return false;
      }
      else
      return true;
    }
  }
-->
</SCRIPT>

Westbury

If it aint broke, redesign it!
 
Thanks Westbury, but it doesn’t work.
 
Hmmmm, here is a copy of the code that I used to test it with.
Code:
<HTML>
<HEAD>
<script>
<!--
function Empty()
{
    if ((document.frmItem.ItemName.value == "") || (document.frmItem.Description.value == "") || (document.frmItem.AskingPrice.value == "") || (document.frmItem.Place.value == ""))
    {
      	alert ("Please complete all fields");
      	return false;
    }
    else
    {
      	if(document.frmItem.Sess.checked)
      	{
          	alert ("Please go to step 3 to add an image");
        	return false;
      	}
      	else
      		return true;

    }
  }
-->
</SCRIPT>
</HEAD>
<form name="frmItem">
<br><input type="textbox" name="ItemName">
<br><input type="textbox" name="Description">
<br><input type="textbox" name="AskingPrice">
<br><input type="textbox" name="Place">
<br><input type="Checkbox" name="Sess">
<br><input type="button" Value="Test" onclick="alert(Empty());">
</html>

I have replaced <%Session("success")%> with a checkbox but thats only because I have no idea where <%Session("success")%> is coming from (I assume is something to do with JSP pages).

If it simeply prints with true or false then I suggest putting brackets around it.
Code:
if(<%Session("success")%>)

Westbury

If it aint broke, redesign it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top