Jump to content

Form Validation


roondog

Recommended Posts

i'm trying a bit of validation on my forms and i'm just trying start simple. There is an eror with this script but not sure what here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /><style type="text/css">#name_er{color:red;display:none;}#email_er{color:red;display:none;}</style><script type="text/javascript">function validForm(){function nameCheck(){	var letters = /^[a-zA-Z]+$/;	if(Document.getElementbyId('name').value.match(letters)){		return true;	}else{		Document.getElementbyId('name_er').style.display = block;		return false;	}}function validEmail(){	var validAddress = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;	if(Document.getElementbyId('email').value.match(validAddress)){		return true;	}else{		Document.getElementbyId('email_er').style.display = block;		return false;	}}if(nameCheck() && PcCheck() && validEmail()){document.form.submit();  } }</script></head><body><form action="http://www.roondogid.co.uk/php.mail.php" method="post"><p>Name:<br /><input type="text" id="name" /></p><p id="name_er">Invalid Name</p><p>Email:<br /><input type="text" id="email" /></p><p id="email_er">Invalid Email</p><input type="button" value="Send Form" onCLick = validform(this.form); /></form>

Link to comment
Share on other sites

What is with the nested functions? Also, the document object has a lowercase d. (There are also some errors with your form, dealt with later). Try

function validForm(){if(nameCheck() && PcCheck() && validEmail()) return true;else return false;}function nameCheck(){var letters = /^[a-zA-Z]+$/;if(document.getElementbyId('name').value.match(letters)){return true;}else{document.getElementbyId('name_er').style.display = block;return false;}}function validEmail(){var validAddress = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;if(document.getElementbyId('email').value.match(validAddress)){return true;}else{document.getElementbyId('email_er').style.display = block;return false;}}

You also need PcCheck() defined for it to work.By the way, it is more secure to use the onsubmit handler of the form. So your form looks like

<form action="http://www.roondogid.co.uk/php.mail.php" method="post" onsubmit="return validForm()"><p>Name:<br /><input type="text" id="name" /></p><p id="name_er">Invalid Name</p><p>Email:<br /><input type="text" id="email" /></p><p id="email_er">Invalid Email</p><input type="submit" value="Send Form" /></form>

Link to comment
Share on other sites

Thanks my brain really isn't in gear this morning. I'll try it later.it needed a bit more tweeking but is now working fine.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...