我需要使用正则表达式验证输入到输入文本框中的电子邮件地址。
如何使用正则表达式\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
与输入的电子邮件ID进行比较?
请建议。
答案 0 :(得分:1)
电子邮件地址验证是一个永无止境的故事。即使您可以设法找到一个匹配有效地址的模式,但这并不意味着该地址确实有效,并且接收器已存在。
特别是对于即将推出的新顶级域名,最后常用的[A-Z]{2,4}
将会过时。(somebody@company.technology
)此外,有些公司会根据子域使用电子邮件地址(即{{ 1}}),会在域中产生多个点。
因此,对电子邮件地址的唯一有效检查是查找是否存在@符号,并且可能是非空字符串 - 以及无效字符。
答案 1 :(得分:0)
试试这个
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');
}
答案 2 :(得分:0)
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
}
这项工作顺利而精致
答案 3 :(得分:0)
根据评论中指出的reference,这应该有效 -
/*
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
*/