How you can perform an email-id validation in HTML by using javascript?

Tripati_tutu
Posted by Tripati_tutu under JavaScript category on | Points: 40 | Views : 2625
To perform an email-id validation you need to focus on the below conditions.
A) Dotnetfunda.com => "@" must be exist => str.indexOf("@") =>k1<0
B) Dotnet funda.com => space must not exist => str.indexOf(" ") => k2>=0
C) Dotnetfunda@.com => must be some character between '@' and '.'
=> str.indexOf(".") =>k3 (let's say k3=7)
str.indexOf("@") => k1 (let's say k1=6). So no space between 6 and 7.
For this we have to put a condition k4=k3-k1==1

Code Snippet:
<!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>
<title></title>
<script type ="text/javascript" language="javascript">
function f1()
{
str = f.t1.value
k1 = str.indexOf("@")
k2 = str.indexOf(" ")
k3 = str.indexOf(".")
k4 = k3 - k1
if (k1 < 0 || k2 >= 0 || k4 == 1)
{
alert("invalid email-id format")
f.t1.focus()
}
}
</script>
</head>
<body>
<form action ="" name="f">
Email-ID: <input type="text" name="t1" onblur="f1()" /><br/>
Password: <input type="password" name="t2" /><br/>
<input type ="button" name="b1" value="Submit" />
</form>
</body>
</html>


Here "indexOf()" is used to get index of specified character.

Comments or Responses

Login to post response