Forum Moderators: open

Message Too Old, No Replies

Can't validate my form!

Always return me an alert box with UNDEFINED

         

tomda

1:22 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

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> &nbsp;&nbsp;&nbsp; <input type=reset value="Clear Form" class="tiny form1"></p></td></tr></table></form>
</div>
</body></html>

Edit - remove more unnecessary code

tomda

2:03 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Finally found the problem!
The function should always return true or false.
And the onsubmit should be "return function();"


<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();"> 

Bernard Marx

2:08 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) You have
return false
in the
onsubmit
handler. The form can never be submitted.
Need:

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;
}
}