Picnic Website Code Tutorials

Simple Javascript Form Validation Tutorial

View Demo

First, add onsubmit="return checkform(this);" to the <form> tag so it looks like the snippit below.

<form method="post" action="formmail.php" onsubmit="return checkform(this);">
		

Second, place this javascript within the <head> of your document. Voila - your done!

<script type="text/javascript">
<!--
function checkform ( form )
{
 // ** START **
   if (form.name.value == "") {
    alert( "Please enter your name." );
    form.name.focus();
    return false ;
  }
  if (form.email.value == "") {
    alert( "Please enter your email address." );
    form.email.focus();
    return false ;
  }
   if (form.subject.value == "") {
    alert( "Please enter the subject." );
    form.subject.focus();
    return false ;
  }
  if (form.comments.value == "") {
    alert( "Please enter your comments." );
    form.comments.focus();
    return false ;
  }
  // ** END **
  return true ;
}
//-->
</script>
		

Need help adding this or other code to your website? Post it here in the forum! Forum

Sponsors

Top Donators

Friends of Mine