PDA

View Full Version : Form Validation


dawn
05-23-2003, 11:16 AM
I have put the following script in the head of my HTML to validate a form. It works fine but it is still submitting the form even though the fields aren't validated. I thought 'return false' would stop the for from being submitted

Can anyone help me out please?

function validate(){
if (document.form1.RealName.value.length < 1){
alert("Please enter your first name.");
return false;
}

if (document.form1.Surname.value.length < 1){
alert("Please enter your Surname.");
return false;

}
var addy=document.form1.From.value

at=addy.indexOf("@",0);
dot=addy.lastIndexOf(".");
uname=addy.substring(0,at);
udomain=addy.substring(at+1,dot)
utld=addy.substring(dot+1,addy.length)

if(at==-1) {

tellEm();
return false();}

else if(uname.length < 0) {

tellEm();
return false;}

else if(udomain.length < 0) {

tellEm();
return false;}

else if(utld.length < 2) {

tellEm();
return false;}

else if((dot==-1) || (dot<at)) {

tellEm();
return false;}


function tellEm() {
alert("Please enter a valid email address.");
}


//-->


return true
}



This is the action script:
<form name="form1" method="post" action="http://cgidirectory/cgi-bin/clanf2m.pl" onSubmit="validate();">

dawn
05-23-2003, 12:22 PM
I have now fixed half of the problem. The book I'm learning from had an error in it. onSubmit="validate();" should be onSubmit="return validate();".

However, the validate email part of the script was from another JS resource site and that part is still submitting even when it hasn't been validated. I don't know enough yet to be able to see where I may be going wrong.

Can anyone point me in the right direction?

arnyinc
05-23-2003, 01:21 PM
It might just be the false() thing. This code works:

<html>
<head>
<script language="javascript">
function validate(myform){
if (myform.RealName.value.length < 1){
alert("Please enter your first name.");
return false;
}

if (myform.Surname.value.length < 1){
alert("Please enter your Surname.");
return false;

}
var addy=myform.From.value

at=addy.indexOf("@",0);
dot=addy.lastIndexOf(".");
uname=addy.substring(0,at);
udomain=addy.substring(at+1,dot)
utld=addy.substring(dot+1,addy.length)

if(at==-1) {

tellEm();
return false;}

else if(uname.length < 0) {

tellEm();
return false;}

else if(udomain.length < 0) {

tellEm();
return false;}

else if(utld.length < 2) {

tellEm();
return false;}

else if((dot==-1) || (dot<at)) {

tellEm();
return false;}


function tellEm() {
alert("Please enter a valid email address.");
}


//-->


return true
}
</script>
</head>
<body>

<form name="form1" method="post" action="http://cgidirectory/cgi-bin/clanf2m.pl" onSubmit="return validate(this);">
<input type="text" name="RealName">
<input type="text" name="Surname">
<input type="text" name="From">
<input type="submit">
</form>
</body>
</html>

dawn
05-23-2003, 02:21 PM
Thankyou very much for sorting out my problem. :thumbsup:

I'm going to go through the script in more detail when I get a chance to understand a bit more what is going on, so I may have a few more questions then.

Thanks again