Email Validation in Javascript

In this tutorial you will learn email validation in javascript. While developing a form for web page, it is common requirement to validate the email address. Now let's look how to implement email validation in javascript.

<html>
<head>
<title>Email validation in javascript</title>


<script language="javascript" type="text/javascript">
function checkEmail() {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form1.users_email.value)){
return (true)
}
alert("Please enter valid email address");
form1.users_email.focus();
return (false)
}
</script>

</head>


Now let’s see how to call this function to achieve validation of email address in javascript. I am going to call this function on button's onClick event.


<body>
<form name="form1">
<input type="text" name="users_email" id="users_email" class="boxpink"/><br />
<input type="button" value="submit" onclick="return checkEmail();">
</form>
</body>
</html>

So this is the way to implement email validation in javascript.

0 comments: