Forum Moderators: open
Just want to validate a simple form, but it can't.
I always get an UNDEFINED alert message despite the fact that all fiels are fine.
Any help would be highly appreciated?
Just copy and paste the form below and try it?
Thank you
NOTE: HTML has been valildated and fine.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>Undefined</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="robots" content="none">
<script type="text/javascript">
<!--
var message="";
var pass="true";
function verify() {
if (document.form1.form_username.value=="") {var message="You forgot to complete your USERNAME"; pass="false";}
if (pass=="false") {alert(message + " " + pass); return false;} else {return true;}}
// -->
</script>
</head>
<body>
<div id=container>
<form name="form1" method="post" action="register.php" onsubmit="verify(); return false;">
<table cellspacing=0 cellpadding=0 class=full>
<tr><td width=137><p><b><font color="#FF0033">*</font> Desired Username:</b></p></td>
<td colspan=2><p class=note><input name="form_username" type="text" value="" class=form1 maxlength=15></p></td></tr>
<tr><td colspan=2><p>
<input type="submit" name="Submit" value="JOIN NOW!" class=form1> <input type=reset value="Clear Form" class="tiny form1"></p></td></tr></table></form>
</div>
</body></html> Edit - remove more unnecessary code
<script type="text/javascript">
<!--
function verify() {
if (document.form1.form_username.value=="") {alert("You forgot to complete your USERNAME"); return false;}
return true;}
// -->
</script>
<form name="form1" method="post" action="register.php" onsubmit="return verify();">
return false in the onsubmit handler. The form can never be submitted. onsubmit="return verify(this);" The
this is a reference to the form. It makes it easier to reference elements inside the function. 2) If the validation fails on the first try, the form can never be submitted because the variable, pass, is never set to true again.
function verify(form)
{
if (form.form_username.value=="")
{
alert( "You forgot to complete your USERNAME" );
return false;
}
else
{
return true;
}
}