Hi everyone My problem is that i am using javascript form validation for the first time and have made a function "Validateform()" to validate "emailid" field in my Form.
Now the problem is that it's not working.

Now in my registeration form when i enter a wrong email id for e.g ppl ,it still inserts it in database rather than giving an alert as specified in function ValidateForm() in my 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" xml:lang="en" lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="" />

	<title>Doctor Registeration Form</title>
    <script language="javascript" type="text/javascript" >
	
	function validateForm(elam)
{
    
    //var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z].[2-5];
	var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/
    if(filter.match(elam))
	{
     return true;
	    }
    else
    {
        alert("ïnvalid email id");
        return false;
    }
}
    
    </script>
</head>

<link rel="stylesheet" href="experimentcss.css" />
<body>
<table width="1100" height="800" border="1" cellpadding="0" cellspacing="0" align="center" >

  <!----1ST ROW OF MAIN TABLE---->


   <tr>
    <td><br />
    <?php
include("blogic.php");
if(isset($_POST['submit']))
{
	$a=$_POST['a'];
    $name1=$_POST['name'];
    $name=$a.$name1;
    $address1=$_POST['address1'];
    $address2=$_POST['b'];
    $address=$address1.",".$address2;
    $gender=$_POST['gender'];
    $age=$_POST['age'];
    $username=$_POST['username'];
    $emailid=$_POST['emailid'];
    $password=$_POST['password'];
    $confirmpassword=$_POST['confirmpassword'];
	$specialization=$_POST['specialization'];
if($password==$confirmpassword)
 {if(isset($_POST['check']))
  {
	connection();
    $qr="insert into doctor values('$name','$address','$gender',$age,'$specialization','$username','$emailid','$password')";
    insert($qr);
    echo "<br><a href='doctor.html'>Go Back To Login Page</a>";
	mysql_close($link);
  }
  else
  {die("Plz check on Term and Condition option ");}
 }
else{die("password and confirmpassword should be same");}
}
else
{
 ?>
 <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" onsubmit="return validateForm(document.getElementById("emailid"))">
 <table cellpadding="10" cellspacing="10" align="center">
 <caption>DOCTOR REGISTRATION FORM</caption>
 <tr><td align="left">NAME<sup>*</sup></td><td>:<select name="a"> <option value="MR.">MR.</option><option value="MRS.">MRS.</option><option value= "MISS.">MISS.</option></select><input type="text" name="name" size="20"/></td></tr>
 <tr><td align="left">ADDRESS<sup>*</sup></td><td>:<input type="text" name="address1" size="20"/></td></tr>
 <tr><td align="left">SECTOR</td><td>:<select name="b"><option value="sector-1">SECTOR-1</option>
 <option value="sector-2">SECTOR-2</option>
 <option value="sector-3">SECTOR-3</option>
 <option value="sector-4">SECTOR-4</option>
 <option value="sector-5">SECTOR-5</option>
 <option value="sector-6">SECTOR-6</option>
 <option value="sector-7">SECTOR-7</option>
 <option value="sector-8">SECTOR-8</option>
 <option value="sector-9">SECTOR-9</option>
 <option value="sector-10">SECTOR-10</option>
 </select></td></tr>
 <tr><td align="left">GENDER <sup>*</sup></td><td>:<input type="radio" name="gender" value="Male"/>MALE<input type="radio" name="gender" value= "Female" />FEMALE</td></tr>
 <tr><td align="left">AGE <sup>*</sup></td><td>:<input type="text" name="age" size="20"/></td></tr>
  <tr><td align="left">SPECIALIZATION<sup>*</sup></td><td>
 <select name="specialization"> <option value="cardiology">Cardiologist</option>
 <option value="Dermatology">Dermatology</option><option value= "Dentist">Dentist</option>
 <option value="Endocrinologists">Endocrinologists</option><option value= "Ent">Ent</option>
 <option value="General">General Physician</option><option value= "Gastroenterologists">Gastroenterologists</option>
 <option value="Orthopedic">Orthopedic</option><option value= "Pediatricians">Pediatricians</option>
 <option value="Psychiatrists">Psychiatrists</option><option value= "Pulmonologist">Pulmonologist</option>
 <option value="Urologists">Urologists</option><option value= "Emergency Doctors">Emergency Doctors</option>
 <option value="Veterinarian">Veterinarian</option>
 
 </select>
 
 </td></tr>
 <tr><td align="left">USERNAME<sup>*</sup></td><td>:<input type="text" name="username" size="20"/></td></tr>
 <tr><td align="left">EMAIL ID <sup>*</sup></td><td>:<input type="text" name="emailid" size="20" id="emailid"/></td></tr>
 <tr><td align="left">PASSWORD <sup>*</sup></td><td>:<input type="password" name="password" size="20"/></td></tr>
 <tr><td align="left">CONFIRM PASSWORD <sup>*</sup></td><td>:<input type="password" name="confirmpassword" size="20"/></td></tr> 
 <tr><td></td><td><input type="checkbox"  name="check" value="check"/>I have read and accept all terms of use</td></tr>
 </table><br />
 <center><input type="submit" name="submit" value="Save&Exit" style="background-color: aqua;" /></center>
 <?php
}
?>
 </form>
    <br />
    </td>
  </tr>
  
</table>


</body>
</html>

try this

<!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" xml:lang="en" lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="" />

	<title>Doctor Registeration Form</title>
    <script language="javascript" type="text/javascript" >
	
	function validateForm()
{
    
    //var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z].[2-5];
	if(document.getElementById('emailid').value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
				{
					alert("Enter valid Email Address");
					document.getElementById('emailid').focus();
					return false;
				}
}
    
    </script>
</head>

<link rel="stylesheet" href="experimentcss.css" />
<body>
<table width="1100" height="800" border="1" cellpadding="0" cellspacing="0" align="center" >

  <!----1ST ROW OF MAIN TABLE---->


   <tr>
    <td><br />
    <?php
include("blogic.php");
if(isset($_POST['submit']))
{
	$a=$_POST['a'];
    $name1=$_POST['name'];
    $name=$a.$name1;
    $address1=$_POST['address1'];
    $address2=$_POST['b'];
    $address=$address1.",".$address2;
    $gender=$_POST['gender'];
    $age=$_POST['age'];
    $username=$_POST['username'];
    $emailid=$_POST['emailid'];
    $password=$_POST['password'];
    $confirmpassword=$_POST['confirmpassword'];
	$specialization=$_POST['specialization'];
if($password==$confirmpassword)
 {if(isset($_POST['check']))
  {
	connection();
    $qr="insert into doctor values('$name','$address','$gender',$age,'$specialization','$username','$emailid','$password')";
    insert($qr);
    echo "<br><a href='doctor.html'>Go Back To Login Page</a>";
	mysql_close($link);
  }
  else
  {die("Plz check on Term and Condition option ");}
 }
else{die("password and confirmpassword should be same");}
}
else
{
 ?>
 <form action="<?php $_SERVER['PHP_SELF'];?>" method="post" onsubmit="return validateForm()">
 <table cellpadding="10" cellspacing="10" align="center">
 <caption>DOCTOR REGISTRATION FORM</caption>
 <tr><td align="left">NAME<sup>*</sup></td><td>:<select name="a"> <option value="MR.">MR.</option><option value="MRS.">MRS.</option><option value= "MISS.">MISS.</option></select><input type="text" name="name" size="20"/></td></tr>
 <tr><td align="left">ADDRESS<sup>*</sup></td><td>:<input type="text" name="address1" size="20"/></td></tr>
 <tr><td align="left">SECTOR</td><td>:<select name="b"><option value="sector-1">SECTOR-1</option>
 <option value="sector-2">SECTOR-2</option>
 <option value="sector-3">SECTOR-3</option>
 <option value="sector-4">SECTOR-4</option>
 <option value="sector-5">SECTOR-5</option>
 <option value="sector-6">SECTOR-6</option>
 <option value="sector-7">SECTOR-7</option>
 <option value="sector-8">SECTOR-8</option>
 <option value="sector-9">SECTOR-9</option>
 <option value="sector-10">SECTOR-10</option>
 </select></td></tr>
 <tr><td align="left">GENDER <sup>*</sup></td><td>:<input type="radio" name="gender" value="Male"/>MALE<input type="radio" name="gender" value= "Female" />FEMALE</td></tr>
 <tr><td align="left">AGE <sup>*</sup></td><td>:<input type="text" name="age" size="20"/></td></tr>
  <tr><td align="left">SPECIALIZATION<sup>*</sup></td><td>
 <select name="specialization"> <option value="cardiology">Cardiologist</option>
 <option value="Dermatology">Dermatology</option><option value= "Dentist">Dentist</option>
 <option value="Endocrinologists">Endocrinologists</option><option value= "Ent">Ent</option>
 <option value="General">General Physician</option><option value= "Gastroenterologists">Gastroenterologists</option>
 <option value="Orthopedic">Orthopedic</option><option value= "Pediatricians">Pediatricians</option>
 <option value="Psychiatrists">Psychiatrists</option><option value= "Pulmonologist">Pulmonologist</option>
 <option value="Urologists">Urologists</option><option value= "Emergency Doctors">Emergency Doctors</option>
 <option value="Veterinarian">Veterinarian</option>
 
 </select>
 
 </td></tr>
 <tr><td align="left">USERNAME<sup>*</sup></td><td>:<input type="text" name="username" size="20"/></td></tr>
 <tr><td align="left">EMAIL ID <sup>*</sup></td><td>:<input type="text" name="emailid" size="20" id="emailid"/></td></tr>
 <tr><td align="left">PASSWORD <sup>*</sup></td><td>:<input type="password" name="password" size="20"/></td></tr>
 <tr><td align="left">CONFIRM PASSWORD <sup>*</sup></td><td>:<input type="password" name="confirmpassword" size="20"/></td></tr> 
 <tr><td></td><td><input type="checkbox"  name="check" value="check"/>I have read and accept all terms of use</td></tr>
 </table><br />
 <center><input type="submit" name="submit" value="Save&Exit" style="background-color: aqua;" /></center>
 <?php
}
?>
 </form>
    <br />
    </td>
  </tr>
  
</table>


</body>
</html>
commented: thanks for help +1

Thanks Karthik.R
So that means the match() function caused it to not work ?

No, it means that the regex was incorrect. Dots (.) need to be escaped (\.) because otherwise, they allow any character.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.