Click to See Complete Forum and Search --> : Form Validation Problem


Nell
04-20-2005, 09:49 AM
I'm trying to validate a form using javascript, but it is not working at all, and the form submit button just proceeds as if the javascript isnt there, can anyone help...

<script type="text/javascript">
function checkform() {
var theform="document.mailform";

if (theform.from.value=="" ) {
alert("Sorry, you need to enter your email address");
theform.from.focus();
return false;
}
return true;
}
</script>

<form action="scripts/sendcommentmail.php" method="get" name"mailform" onsubmit="return checkform()">
<table cellspacing="5" border="0" align="center">
<tr>
<td colspan="2">
<h1>Contact Us!</h1>
If you would like to contact use about anything to do with the P&L reptiles including information on availible morphs, site comments / suggestions, technical information etc, please send an email to <a href="mailto:james.neilson@northumbria.ac.uk">james.neilson@northumbria.ac.uk</a> or for conveinience simply fill out the contact form below.<br /><br /><br />
</td>
</tr>
<tr>
<td>From:</td>
<td><input type="text" name="from" value=""/></td>

</tr>
<tr>
<td>Comment:</td>
<td><textarea name="comments" cols="30" rows="6" value=""> </textarea></td>
</tr>
<tr>
<td>Click To Send / Reset</td>
<td>
<input type="submit" name="send" value="Send" />
</td>
</tr>



</table>
</form>

phpnovice
04-20-2005, 09:55 AM
This:

var theform="document.mailform";

should be this:

var theform=document.mailform;

Nell
04-21-2005, 08:10 AM
it still doesn't do anything even when i change that :(

scragar
04-21-2005, 08:15 AM
try:<script type="text/javascript"><!--
function checkform() {
var theform=document.forms[0];
if (theform.inputs[0].value == "") {
alert("Sorry, you need to enter your email address");
theform.inputs[0].focus();
return false;
}

return true;
}
//-->
</script>

phpnovice
04-21-2005, 08:26 AM
If no DOCTYPE in your page, this:

<script type="text/javascript">

should probably be this:

<script language="javascript" type="text/javascript">

Nell
04-21-2005, 02:15 PM
still no luck with either adjustments.

i've uploaded the file to:

http://www.nl-webspace.co.uk/~unn_p596565/html/contact.html

none of the other files / graphics are uploaded so it wont display like it should obviously.

phpnovice
04-21-2005, 03:47 PM
Try changing your script tag to this anyway:

<script language="javascript" type="text/javascript">

Charles
04-21-2005, 03:55 PM
If no DOCTYPE in your page, this:

<script type="text/javascript">

should probably be this:

<script language="javascript" type="text/javascript">Currently HTML is HTML 4.01 and in HTML 4.01 there is no "language (http://www.w3.org/TR/html401/interact/scripts.html#adef-language)" attribute. It only exists in certain legacy versions of HTML and it has been deprecated. By "deprecated (http://www.w3.org/TR/html401/conform.html#deprecated)" the W3C means that they are asing us to stop using it.

phpnovice
04-21-2005, 04:29 PM
...means that they are asing us to stop using it.
That is fine and dandy, but if the browser won't execute the JavaScript without it, then you have no choice. I have actually seen this happen. It might have only been in IE, I don't remember (and it doesn't really matter because browsers are supposed to basically ignore attributes they don't recognize), but it did happen. I had this:

<script type="text/javascript">

and my script wasn't executing. I changed only this:

<script language="javascript" type="text/javascript">

and it worked perfectly after that.

Pelle
04-21-2005, 05:02 PM
Now the code works, but don't ask me why the other code didn't, because I don't have a clue really. I worked on it quite some time and compared it to some similar (working) code I had. I found no differences so the code really should have have worked. At last I took some old code I had and modified it to some extent (to fit your problem). If you find any important differences (the reason the old code didn't work :) ) between this working code and the other non-working code, I would like to read about it... Enjoy!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<script language="JavaScript" type="text/JavaScript">
<!--

function checkform(){
if(document.mailform.from.value==""){ alert("Sorry, you need to enter your email address"); document.mailform.from.focus(); return false; }
else { return true; }
}
//-->
</script>
</head>

<body>
<form action="scripts/sendcommentmail.php" method="get" name="mailform" onSubmit=" return checkform()">
<table width="454" height="140" border="1">
<tr>
<td width="163">From:</td>
<td width="275">
<input name="from" type="text" id="text13" size="40" maxlength="40" width="100%">
</td>
</tr>
<tr>
<td>Comment:</td>
<td>
<textarea name="comments" cols="40" rows="5" id="textarea1"></textarea></td>
</tr>
<tr>
<td>Click To Send / Reset</td>
<td><input type="submit" name="Submit" value="Send" align="right"></td>
</tr>
</table>
</form>
</body>
</html>

Nell
04-22-2005, 07:51 AM
works perfect, many thanks!