Hy guys i know there is one thread about validation ...
but i cant find what is messing up this simple form ...
cant get correct result .. iam new in javascript so if you have any idea or what is wrong would you let me know .. i understand this script but iam not sure if its correct.. THanks guys..
<!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>Emerald Bank</title>
<style type="text/css">
.table{
border:3px black solid;
width:300px;
margin-left:100px;
padding:20px;
}
.table tr{
height:40px;
}
.table td{
border-bottom:1px solid black;
height:37px;
}
</style>
</head>
<body>
<form name="verification" onsubmit="return validate();" method="post">
<div id="container">
<h1>Registration</h1>
<div class="table">
<table>
<tr>
<td>*First Name:</td><td><input type="text" maxlength="50" id="firstname" /></td>
</tr>
<tr>
<td>*Second Name:</td><td><input type="text" maxlength="50" id="surename" /></td>
</tr>
<tr>
<td>*Gender:</td><td><label for="male">Male</label><input type="radio" name="gender" id="male"/></input> <label for="female">Female</label><input type="radio" name="gender" id="female"/></input></td>
</tr>
<tr>
<td>*Email:</td><td><input type="text" maxlength="50" id="mail" /></td>
</tr>
<tr>
<td>*City:</td><td><input type="text" maxlength="50" id="city" /></td>
</tr>
<tr>
<td>*Street:</td><td><input type="text" maxlength="50" id="street" /></td>
</tr>
<tr>
<td>*Post code:</td><td><input type="text" maxlength="50" id="post" /></td>
</tr>
<tr>
<td><input type="submit" value="Verify" /></input> <input type="reset" id="reset" /></input></td>
</tr>
</table>
</form>
</div>
</div>
</body>
<script type="text/javascript">
document.getElementById('firstname').focus()
function validate(){
var x = document.forms["verification"] ["firstname"].value;
var y = document.forms["verification"] ["surename"].value;
var a = document.forms["verification"] ["mail"].value;
var b = document.forms["verification"] ["city"].value;
var c = document.forms["verification"] ["street"].value;
var d = document.forms["verification"] ["post"].value;
var name = document.getElementById('firstname').value;
var sname = document.getElementById('surename').value;
var atpos = a.indexOf("@");
var dotpos = a.indexOf(".");
/* conditions*/
if (x==null ||x==" ")
{
alert("First name must by filled out");
return false;
}
else if (y==null ||y==" ")
{
alert("Surename must by filled out");
return false;
}
else if(atpos<1 || dotpos<atpos+2 || dotpos>=a.length) {
alert("Not a valid email address");
return false;
}
else if (b==null ||b==" ")
{
alert("City field must by filled out");
return false;
}
else if (c==null || c==" ")
{
alert("Street field must by filled out");
return false;
}
else if (d==null || d==" ")
{
alert("Post code must by filled out");
return false;
}
else{
alert("Thank you for registration" +firstname+ +surename);
}
}
</script>
</html>