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!

problem with validation of a simple form Help

Status
Not open for further replies.

hemal666

Programmer
Dec 10, 2001
25
GB
Hi i ve just writen some code to validate a simple form

the JS is not working at all, im completely lost please help!
heres the code::

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<title>Untitled Document</title>
<SCRIPT LANGUAGE=&quot;JAVASCRIPT&quot; TYPE=&quot;TEXT/JAVASCRIPT&quot;>
<!-- Hide Script From Old Browsers
function validfname(fname) {
if (fname == &quot;&quot;) {
return false // Check not empty
}
return true
}
function validsname(sname) {
if (sname == &quot;&quot;) {
return false // Check not empty
}
return true
}

function submitform(check) {
if(!validfname(check.fname.value)) {
alert(&quot;No First Name Entered&quot;)
check.fname.focus()
check.fname.select()
return false
}
if(!validsname(check.sname.value)) {
alert(&quot;No First Name Entered&quot;)
check.sname.focus()
check.sname.select()
return false
}
else
return true
}
// end hiding -->
</SCRIPT>
</head>
<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot;>
<CENTER>
<p>
Please Enter the Following Details
<p>
Boxes marked with an asterix are Required
<FORM METHOD=POST onSubmit=&quot;Submitform(this)&quot; ACTION=&quot;registerinterest.asp&quot;>
First Name <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;fname&quot; SIZE=&quot;20&quot;>
<P>
Surname <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;sname&quot; SIZE=&quot;20&quot;>
<P>
House Number <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;hfno&quot; SIZE=&quot;4&quot;>
<p>
Address Line 1 <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;add1&quot; SIZE=&quot;50&quot;>
<P>
Address Line 2 <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;add2&quot; SIZE=&quot;50&quot;>
<P>
Post Code <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;pcode&quot; SIZE=&quot;8&quot;>
<P>
Day Time Tel <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;daytel&quot; SIZE=&quot;12&quot;>
<P>
Evening Tel <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;evetel&quot; SIZE=&quot;12&quot;>
<P>
Mob Tel <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;mobtel&quot; SIZE=&quot;12&quot;>
<P>
Best Time to Contact You <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;btoc&quot; SIZE=&quot;30&quot;>
<P>
<TEXTAREA NAME=&quot;detreq&quot; ROWS=&quot;3&quot; COLS=&quot;75&quot;>Your Requirments.......... This section is MANDATORY!!</TEXTAREA>
<P>
<INPUT TYPE=RESET><INPUT TYPE=&quot;SUBMIT&quot;>
</FORM>
<CENTER>
</body>
</html>
 
hemal,
you might try onSubmit=&quot;return Submitform(this)&quot;

good luck,

tsmith
 
There are several errors in your code.

1. In your validsname() and validfname() functions, instead of this:
. . .
}
return true
}

do this:
. . .
}
else
return true
}

2. In html you have this: onSubmit=&quot;Submitform(this)&quot;
but in your script I see this: function submitform(check)
Javascript is case-sensitive language, therefore &quot;Submitform&quot; and &quot;submitform&quot; are two different things. Make it all the same (I recommend changing all to lowercase).

3. Change onSubmit=&quot;submitform(this)&quot;
to onSubmit=&quot;return submitform(this)&quot;

4. If the field is empty, I don't know why you do this:
check.sname.select()
as there's nothing to select there.

Also, I'd recommend you to change the validation method to more simple, like looping through the form elements and checking their values. Search this forum and you'll find examples (I posted them several times).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top