What is the best Java email address validation method?

Validation Java Script Method.

<html>
<head></head>
<script>
function valid()
{
var name=document.getElementById("name").value;
var email=document.getElementById("email").value;
var password=document.getElementById("password").value;
var male=document.getElementById("male").checked;
var female=document.getElementById("female").checked;
var sports=document.getElementById("sports").checked;
var reading=document.getElementById("reading").checked;
var sq=document.getElementById("sq").value;
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");

//alert(dotpos);

if(name=="")
{
alert("enter username");
return false;
}


if(email=="")
{
alert("enter email");
return false;
}
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) {
alert("Not a valid e-mail address");
return false;
}
if(password=="")
{
alert("enter password");
return false;
}
if(male=="" && female=="")
{
alert("Select the gender");
return false;
}
if(sports=="" && reading=="")
{
alert("Select the hobbies");
return false;
}
if(sq=="")
{
alert("Select the security question");
return false;
}
return true;
}

</script>


<body>
<form action="" method="post">
<table border="1" height="500" width="500" align="center">
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" value=""></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"id="email" value="" placeholder="[email protected]"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"id="password" value=""></td>
</tr>
<tr >
<td >Gender</td>
<td>Male<input type="radio" name="male" id="male" value="male">
Female<input type="radio" name="female" id="female" value="female"></td>
</tr>
<tr>
<td>Hobbies</td>
<td>sports<input type="checkbox" value="sports" name="sports" id="sports">
reading<input type="checkbox" value="reading" name="reading" id="reading">
music<input type="checkbox" value="music" name="music" id="music">
Cricket<input type="checkbox" value="Cricket" name="Cricket" id="cricket">
</td>
</tr>
<tr>
<td>SECURITY QUESTION</td>
<td>
<select id="sq">

<option value="">your favorite Teacher?</option>
<option>your favorite Game?</option>
<option>your favorite food</option>
<option>your favorite Gym</option>
<option>your favorite food</option>
</select>

</td>
</tr>
<tr>
<td>ANSWER</td>
<td><input type="text" name="answer" value=""></td>
</tr>
</tr>
<tr>
<td>FILE</td>
<td><input type="file" name="image" value=""></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit" onclick="return valid();">
<input type="reset" value="reset"></td>
</tr>






</table>
</form>


</body>
</html>
 
Back
Top