I need to validate email address entered into a input textbox using regex.
How can I use regex \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
to compare with the entered email id?
Pls suggest.
I need to validate email address entered into a input textbox using regex.
How can I use regex \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
to compare with the entered email id?
Pls suggest.
Email-Address validation is a never ending story. Even if you could manage to find a pattern, that does match valid addresses, that doesn't mean, that the address is really valid, and the receiver is existing.
Especially with the new upcoming top-level-domains the commonly used [A-Z]{2,4}
at the end will be obsolete.(i.e. somebody@company.technology
) Also some companies use email-addresses depending on subdomains (i.e. somebody@division.company.tld
), which would produce multiple dots in the domain.
Therefore the ONLY valid check on email adresses would be to look for the presence of an @ sign and maybe non-empty strings around it - and invalid characters.
Try this
function validateEmail(email) {
var re = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b/;
return re.test(email);
}
var email = document.getElementById("email").value;
if ( validateEmail(email) ) {
alert('valid');
} else {
alert('invalid');
}
function echeck(){
var at="@"
var dot="."
var str=document.registerapplicant.regtxtemail.value;
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if(str=='')
{
return false
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
{
alert("Invalid E-mail ID")
document.registerapplicant.regtxtemail.value='';
document.registerapplicant.regtxtemail.focus();
return false
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
{
alert("Invalid E-mail ID")
document.registerapplicant.regtxtemail.value='';
document.registerapplicant.regtxtemail.focus();
return false
}
if (str.indexOf(at,(lat+1))!=-1)
{
alert("Invalid E-mail ID")
document.registerapplicant.regtxtemail.value='';
document.registerapplicant.regtxtemail.focus();
return false
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
{
alert("Invalid E-mail ID")
document.registerapplicant.regtxtemail.value='';
document.registerapplicant.regtxtemail.focus();
return false
}
if (str.indexOf(dot,(lat+2))==-1)
{
alert("Invalid E-mail ID")
document.registerapplicant.regtxtemail.value='';
document.registerapplicant.regtxtemail.focus();
return false
}
if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
document.registerapplicant.regtxtemail.value='';
document.registerapplicant.regtxtemail.focus();
return false
}
//return true
}
this works smooth and fine
According to the reference pointed out to me in the comments, this should work -
/*
I have split the patterns into three different regexs.
*/
function email_valid(str){
var patt1=/(?:[a-zA-Z][a-zA-Z0-9\+-]*(?:[._][a-zA-Z0-9\+-]+)*)\@[a-zA-Z0-9]+(?:\.[a-zA-Z]+)*/g;
var patt2 = /(?:\".*?\")\@[a-zA-Z]+(?:\.[a-zA-Z]+)*/g;
var patt3 = /(?:[a-zA-Z][a-zA-Z0-9\+-]*(?:[._][a-zA-Z0-9\+-]+)*)\@\[IPv6:[0-9a-f]+:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]+\]/g;
if(patt1.exec(str) == str){return true;}
if(patt2.exec(str) == str){return true;}
if(patt3.exec(str) == str){return true;}
return false;
}
/*
This is the output from the reference given. It is not a 100%, but still good enough.
Correct Email-Ids, Answer must be true
niceandsimple@example.com => true
very.common@example.com => true
a.little.lengthy.but.fine@dept.example.com => true
disposable.style.email.with+symbol@example.com => true
other.email-with-dash@example.com => true
user@[IPv6:2001:db8:1ff::a0b:dbd0] => true
"much.more unusual"@example.com => true
"very.unusual.@.unusual.com"@example.com => true
"very.(),:;<>[]".VERY."very@\ "very".unusual"@strange.example.com => true
postbox@com => true
admin@mailserver1 => true
!#$%&'*+-/=?^_`{}|~@example.org => false
"()<>[]:,;@\"!#$%&'*+-/=?^_`{}| ~.a"@example.org => true
" "@example.org => true
üñîçøðé@example.com => false
üñîçøðé@üñîçøðé.com => false
Incorrect Email-Ids, Answer must be false
Abc.example.com => false
A@b@c@example.com => false
a"b(c)d,e:f;gi[j\k]l@example.com => false
just"not"right@example.com => false
this is"notllowed@example.com => false
this\ still"not\allowed@example.com => false
*/