Validation In JavaScript

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style>
label{
	display:inline-block;
	width:100px;
}
</style>


<script>

function validateForm()
{
	
	
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }



var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }




var x=document.forms["myForm"]["message"].value;

if (x==null || x=="" || x.length<10)
  {
  alert("First message must be filled out and no tless than 10");
  return false;
  }





}





</script>
</head>

<body>


<fieldset style="width:200px; background-color:#CCC;" id="field">
<legend>Conatct Me</legend>
<form name="myForm" action="thanks.php" onsubmit="return validateForm()" method="post">
<label>First name:</label> <input type="text" name="fname"><br />
<label>Email:</label> <input type="text" name="email"><br />
<label>Message : </label> <textarea name="message" >

</textarea><br />
<input type="submit" value="Submit">
</form>
</fieldset>

</body>
</html>


Leave a Reply