Email validation in java script?

Posted by Gaddad under JavaScript on 12/23/2009 | Views : 1425 | Status : [Member] | Replies : 3
Hi,

I need email validation in the format "abc.def@ghi.klm" one dot should be mandatory after "@" symbol.

Thanks in advance!




Responses

Posted by: Subashini on: 6/9/2014 [Member] Starter | Points: 25

Up
0
Down
Look at this example of how it can be done in Javascript:

<html>

<head>
<script type="text/javascript">

<!--
function validateEmail() {
var emailText = document.getElementById('email').value;
var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
if (pattern.test(emailText)) {
return true;
} else {
alert('Bad email address: ' + emailText);
return false;
}
}

window.onload = function() {
document.getElementById('email_form').onsubmit = validateEmail;
}
</script>

</head>
<body>

<form id="email_form">
<input type="text" id="email">
<input type="submit">
</form>

</body>
</html>



www.accordsoft.co.in

Gaddad, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/23/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
create a Function that will validate the e-mail

function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}


and use it like this

<form id="form_id" method="post"  onsubmit="javascript:return validate('form_id','email');">

<input type="text" id="email" name="email" />
<input type="submit" value="Submit" />
</form>


Thank you for posting at dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Gaddad, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rcsprakash on: 1/15/2010 [Member] Starter

Up
0
Down
try this . it will solve your problem

function checkEmail(strReceived)
{
var emailRegex=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!emailRegex.test(strReceived))
{
return false;
}
return true;
}

Gaddad, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response