In this form,i want that when i submit the form it will validate the form and then submitted but here the form is submitted without validating. Whenever i click on submit button, it asks for validation but without validating it is submitted to database. I want onsubmit, it asks for validation and when valid data are inserted then form will submit.
<html>
<head>
<title>Ajax Form</title>
<style>
.right-align{
position: absolute;
right: 70%;
text-align: left;
}
.select_country{
position: absolute;
right: 70%;
width: 160.8;
height: 22;
}
</style>
<head>
<body>
<form name="bhawesh" onsubmit="validate()" method="post" action="formajaxsub.php">
NAME: <input type="text" name="name" class="right-align"><br><br>
FATHER'S NAME: <input type="text" name="father" class="right-align"><br><br>
E-MAIL:<input type="text" name="email" class="right-align"> </font><br><br>
CONTACT:<input type="text" name="contact" class="right-align"> </font><br><br>
COUNTRY:
<select name="country" class="select_country" >
<option>--Select--</option>
<option name="ind">INDIA</option>
<option name="eng">ENGLAND</option>
<option name="chi">CHINA</option>
<option name="aus">AUSTRALIA</option>
</select><br><br>
<input type="submit" Value="SUBMIT" name="submit">
</form>
</body>
<script>
function validate()
{
var a = document.forms["bhawesh"]["name"].value;
if (a == "")
{
alert("Enter the name");
}
var b = document.forms["bhawesh"]["father"].value;
if (b == "")
{
alert("Enter the Father's name");
}
var c = document.forms["bhawesh"]["email"].value;
if (c.indexOf("@")<1 || c.lastIndexOf(".")+2>=c.length || c.indexOf("@")+2>c.lastIndexOf("."))
{
alert("The E-Mail is not OK");
}
var phone = document.forms["bhawesh"]["contact"].value;
if(phone.length > 6 && phone.length < 11)
{
return true;
}
else
{
alert("Contact Number is not OK");
}
var e=document.forms["bhawesh"]["country"].value;
if (e=="")
{
alert("Select the country");
}
}
</script>
</html>