Click to See Complete Forum and Search --> : JAvascript Form validation problems


warkem
10-10-2003, 08:51 AM
Hi,
Keep getting an error on this page,
"Char: 1
Error: Object Expected
Code: 0"

Have no idea why? All the script does is validate the form, I had it working before on another page modified it and now it doesnt work, not even the hello world function??? Here is the code, followed by the function calls:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>AAESS EBP</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

//---------------------------------------------------------------
// SCRIPT
//---------------------------------------------------------------

<SCRIPT language="Javascript">

function showAlert()
{
alert("Hello world!");
}



function resetForm()
{
document.join.reset();
}
//Form Validation
function ValidateSelect()
{
int flag = 0;
//Check for Login
if (document.join.user_id.value == "")
{
alert("Please Enter a Login name.");
document.join.user_id.focus();
return false;
}
//Check for same password entered twice
else if (document.join.password.value != document.join.repassword.value)
{
alert("Please Enter the SAME password twice.");
document.join.repassword.focus();
return false;
}
//Check for password
else if (document.join.password.value == "")
{
alert("Please Enter a password.");
document.join.password.focus();
return false;
}
//Check for re-password
else if (document.join.repassword.value == "")
{
alert("Please retype your password here.");
document.join.repassword.focus();
return false;
}
//Check for Name
else if (document.join.first_name.value == "")
{
alert("Please Enter your first name.");
document.join.first_name.focus();
return false;
}
//Check for Last Name
else if (document.join.last_name.value == "")
{
alert("Please Enter your last name.");
document.join.last_name.focus();
return false;
}
//Check for address
else if (document.join.address.value == "")
{
alert("Please Enter your address.");
document.join.address.focus();
return false;
}

//Check for E-mail
else if (document.join.email.value == "")
{

if (flag != 0)
{
alert("Don't have one?");
alert("Get one!");
alert("Dumbass!");
alert("I mean really who doesn't have an e-mail address???");
document.join.email.focus();
return false;
}
else
{
flag = flag++;
alert("Please Enter you E-Mail address.");
document.join.email.focus();
return false;

}
}

//postcode check
else if (document.join.postcode.value == "")
{
alert("Please Enter your postcode.");
document.join.postcode.focus();
return false;
}

}

</SCRIPT>
<LINK href="aaess.css" type=text/css rel=stylesheet>
<link rel="stylesheet" href="forms.css" type="text/css">
</HEAD>
<BODY leftMargin=0 topMargin=0
onload="MM_preloadImages('images/home_on.gif','images/about_on.gif','images/login_on.gif','images/join_on.gif','images/news_on.gif')"
marginheight="0" marginwidth="0">

//---------------------------------------------------------------
// FUNCTION CALLS
//---------------------------------------------------------------


<form name="join" action="./v3/join.php" method="post" onSubmit="return ValidateSelect(this)">
<p>&nbsp;</p><table cellspacing=0 cellpadding=0 width=400 border=0 align="center">
<tbody>
<tr bgcolor=#009966>
<td colspan=3 height=5><img height=1 src="Join_files/spacer.gif"
width=1></td>
</tr>
<tr bgcolor=#ccffcc>
<td colspan=3 height=10>
<div align=right></div>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td height=30>
<div align=right><font face="Verdana, Arial, Helvetica, sans-serif"
color=#333333 size=1>Login:</font></div>
</td>
<td width=20><img height=1 src="Join_files/spacer.gif" width=1></td>
<td>
<input maxlength=12 size=15 name=user_id class=text>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td height=30>
<div align=right><font face="Verdana, Arial, Helvetica, sans-serif"
color=#333333 size=1>Password:</font></div>
</td>
<td>&nbsp;</td>
<td>
<input maxlength=12 size=15 name=password type="password" class=text>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td height=30>
<div align=right><font face="Verdana, Arial, Helvetica, sans-serif"
color=#333333 size=1>Re-type password:</font></div>
</td>
<td>&nbsp;</td>
<td>
<input maxlength=12 size=15 name=repassword type="password" class=text>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td colspan=2>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr bgcolor=#ff9900>
<td bgcolor=#ff9900 colspan=2 height=40>&nbsp;</td>
<td>
<input type=submit value=Submit name=Submit class="submit">
<input type="reset" name="Submit2" value="Reset" onClick="resetForm()">
</td>
</tr>
</tbody>
</table>
</form>
</TD>
</TR>
</TBODY>
</TABLE>
</DIV></TD></TR>
<TR bgColor=#009966>
<TD colSpan=2 height=20>
<DIV align=center><FONT face="Verdana, Arial, Helvetica, sans-serif"
color=#ffffff size=1>2003 © CSCI321 Project. Group: cs321a42</FONT></DIV>
</TD></TR></TBODY></TABLE>
<input type=submit value=Test name=Submit onclick="showAlert();return false">

</BODY></HTML>

Khalid Ali
10-10-2003, 09:41 AM
you have used JavaScript comments inmultiple places in html which is wrong,and then you are trying to use a function without defining it.
I have fixed the visible errors,did not test form if it works or not.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>AAESS EBP</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<SCRIPT language="Javascript">
<!--
function showAlert()
{
alert("Hello world!");
}



function resetForm()
{
document.join.reset();
}
//Form Validation
function ValidateSelect()
{
var flag = 0;
//Check for Login
if (document.join.user_id.value == "")
{
alert("Please Enter a Login name.");
document.join.user_id.focus();
return false;
}
//Check for same password entered twice
else if (document.join.password.value != document.join.repassword.value)
{
alert("Please Enter the SAME password twice.");
document.join.repassword.focus();
return false;
}
//Check for password
else if (document.join.password.value == "")
{
alert("Please Enter a password.");
document.join.password.focus();
return false;
}
//Check for re-password
else if (document.join.repassword.value == "")
{
alert("Please retype your password here.");
document.join.repassword.focus();
return false;
}
//Check for Name
else if (document.join.first_name.value == "")
{
alert("Please Enter your first name.");
document.join.first_name.focus();
return false;
}
//Check for Last Name
else if (document.join.last_name.value == "")
{
alert("Please Enter your last name.");
document.join.last_name.focus();
return false;
}
//Check for address
else if (document.join.address.value == "")
{
alert("Please Enter your address.");
document.join.address.focus();
return false;
}

//Check for E-mail
else if (document.join.email.value == "")
{

if (flag != 0)
{
alert("Don't have one?");
alert("Get one!");
alert("Dumbass!");
alert("I mean really who doesn't have an e-mail address???");
document.join.email.focus();
return false;
}
else
{
flag = flag++;
alert("Please Enter you E-Mail address.");
document.join.email.focus();
return false;

}
}

//postcode check
else if (document.join.postcode.value == "")
{
alert("Please Enter your postcode.");
document.join.postcode.focus();
return false;
}

}
//-->
</SCRIPT>
<LINK href="aaess.css" type=text/css rel=stylesheet>
<link rel="stylesheet" href="forms.css" type="text/css">
</HEAD>
<!--
THis function was in the body tag,I took it out since there is not definition for this function anywhere
onload="MM_preloadImages('images/home_on.gif','images/about_on.gif','images/login_on.gif','images/join_on.gif','images/news_on.gif')"
//-->
<BODY leftMargin=0 topMargin=0
marginheight="0" marginwidth="0">

<!--
---------------------------------------------------------------
FUNCTION CALLS
---------------------------------------------------------------
//-->


<form name="join" action="./v3/join.php" method="post" onSubmit="return ValidateSelect(this)">
<p> </p><table cellspacing=0 cellpadding=0 width=400 border=0 align="center">
<tbody>
<tr bgcolor=#009966>
<td colspan=3 height=5><img height=1 src="Join_files/spacer.gif"
width=1></td>
</tr>
<tr bgcolor=#ccffcc>
<td colspan=3 height=10>
<div align=right></div>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td height=30>
<div align=right><font face="Verdana, Arial, Helvetica, sans-serif"
color=#333333 size=1>Login:</font></div>
</td>
<td width=20><img height=1 src="Join_files/spacer.gif" width=1></td>
<td>
<input maxlength=12 size=15 name=user_id class=text>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td height=30>
<div align=right><font face="Verdana, Arial, Helvetica, sans-serif"
color=#333333 size=1>Password:</font></div>
</td>
<td> </td>
<td>
<input maxlength=12 size=15 name=password type="password" class=text>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td height=30>
<div align=right><font face="Verdana, Arial, Helvetica, sans-serif"
color=#333333 size=1>Re-type password:</font></div>
</td>
<td> </td>
<td>
<input maxlength=12 size=15 name=repassword type="password" class=text>
</td>
</tr>
<tr bgcolor=#ccffcc>
<td colspan=2> </td>
<td> </td>
</tr>
<tr bgcolor=#ff9900>
<td bgcolor=#ff9900 colspan=2 height=40> </td>
<td>
<input type=submit value=Submit name=Submit class="submit">
<input type="reset" name="Submit2" value="Reset" onClick="resetForm()">
</td>
</tr>
</tbody>
</table>
</form>
</TD>
</TR>
</TBODY>
</TABLE>
</DIV></TD></TR>
<TR bgColor=#009966>
<TD colSpan=2 height=20>
<DIV align=center><FONT face="Verdana, Arial, Helvetica, sans-serif"
color=#ffffff size=1>2003 © CSCI321 Project. Group: cs321a42</FONT></DIV>
</TD></TR></TBODY></TABLE>
<input type=submit value=Test name=Submit onclick="showAlert();return false">

</BODY></HTML>

jbergthorson
10-10-2003, 09:51 AM
I found a number of problems with your code. To sum up:

-you forgot the function MM_preloadImages function but you are calling it.
-use quotes around your variables like: font="2" not an error, but a good idea.
-you used int flag, use var flag
-there is no input last_name or first_name

and i think that is about it... i didn't test it though. try fixing that up and if you are still having problems let me know. Turn on "display script debuggin" in IE(Tools->Internet Options->advanced->Browsing) so that you can see what line there are errors on.

I hope that helps

jason

pyro
10-10-2003, 10:09 AM
You might find it easier to use one of these:

http://www.infinitypages.com/research/formvalidation.htm
http://www.infinitypages.com/research/formvalidation_2.htm

warkem
10-10-2003, 11:03 AM
Thanks Everyone, it was that I hadn't defined my functions!

Daot Lagorille
10-14-2003, 09:06 AM
Sorry to bump this thing, but I just implemented Kalid's fix of this script, and it works beautifully.

Sweetcakes!