Wednesday, 16 April 2014

jQuery Email Validation

jQuery Email Validation
<head>
<title>Email Validation</title>
<link href="css/dcmegamenu.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnSubmit').click(function () {
$(".error").hide();
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#txtEmail").val();
if (emailaddressVal == '') {
$("#txtEmail").after('<span class="error">Please enter email address.</span>');
hasError = true;
}
else if (!emailReg.test(emailaddressVal)) {
$("#txtEmail").after('<span class="error">Please enter a valid email address.</span>');
hasError = true;
}
if (hasError == true) { return false; }
});
});
</script>
</head>
<body>
<input id="txtEmail" type="text" />
<input id="btnSubmit" type="button" value="Submit" />
</body>
</html>