PDA

View Full Version : validating contact form w/js


fmx
02-27-2004, 02:18 PM
I am using a js alert window to verify a contact form.
function verify(){
var name = document.contact.fromname.value;
var email = document.contact.fromemail.value;
var mess = document.contact.message.value;
var outputmess = "Please complete the following field(s):\n___\n"
var errors = "";
if(!name){
errors += "> Your name\n";
}
if(!email){
errors += "> Your email\n";
}
if(!mess){
errors += "> message\n";
}
if(errors){
alert (outputmess + errors);

}
}
<input type="image" src="contactSend.jpg" onClick="verify()" alt="send" name="submitform">

The 2 problems im having is when the alert window does appear and the ‘ok’ button is clicked the form submits. How can I stop this?
And if the form is sent with an email missing a ‘@’ or a ‘.’ I get - page can not be displayed -
what is the best way to verify this?

see it in action --> here (http://dthom.com/contact.html)

schnitzel_21st
02-27-2004, 05:15 PM
just a guess but if you add 'return false' to the error function does that help. Alternativley look on www.javascriptsource.com or www.dynamicdrive.com for a verification script.

-Nigel =)

fmx
02-27-2004, 07:22 PM
no luck with the 'return false'
but thanks for the links.

Willy Duitt
02-27-2004, 07:56 PM
Try this:

<form method="post" action="" onsubmit="return verify()">
<input type="image" src="contactSend.jpg" alt="send" name="submitform">

.....Willy

fmx
02-27-2004, 09:38 PM
still nothing.
what could be wrong?

Willy Duitt
02-27-2004, 10:21 PM
For one. Your function is not even being fired because of a syntax error. Either remove the extra closing bracket after your else statement, or use an opening bracket for your else statement.

.....Willy

fmx
02-27-2004, 11:06 PM
ahh... you cought me in the middle of playing with it. ;)

and yet i tried many ways with no luck. :confused:

Willy Duitt
02-27-2004, 11:39 PM
Now you have removed the onsubmit event handler from the form tag!

Please reread my first post and add the onsubmit back to the form tag and remove the onclick event handler from the <input type="image"> An input type=image is a submit initself.

If you will dilegently try what I have suggested. You will find that it works.

.....Willy

fmx
02-28-2004, 12:23 AM
willy,
when i tried it before i obvisouly screwed it up, and at the end of my spordic 'debugging' i was basicly back where i started. :O@

tried it again and it was successful
thanks