我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
如果你只想做的是抓住最明显的合成错误,我会做这样的事情:
^\S+@\S+$
它通常捕获了用户所犯的最明显的错误,并确保表格主要是正确的,这就是JavaScript验证的所有内容。
EDIT:我们也可以在电子邮件中查看“。
/^\S+@\S+\.\S+$/
其他回答
使用常规表达式:
/^[a-z][a-zA-Z0-9_.]*(\.[a-zA-Z][a-zA-Z0-9_.]*)?@[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/
例子:
function validateEmail(email) {
var re = /^[a-z][a-zA-Z0-9_.]*(\.[a-zA-Z][a-zA-Z0-9_.]*)?@[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/;
return re.test(email);
}
只允许 @,,
如果你只想做的是抓住最明显的合成错误,我会做这样的事情:
^\S+@\S+$
它通常捕获了用户所犯的最明显的错误,并确保表格主要是正确的,这就是JavaScript验证的所有内容。
EDIT:我们也可以在电子邮件中查看“。
/^\S+@\S+\.\S+$/
很难得到一个电子邮件验证器100%正确的唯一真正的方式来得到它正确的将是发送一个测试电子邮件到帐户。
一些要改进的事情:
而不是新的RegExp,只需尝试这样写出regexp:
if (reg.test(/@/))
其次,请确保在 @ 标志之后出现一个时期,并确保 @ 和 期间之间有字符。
<pre>
**The personal_info part contains the following ASCII characters.
1.Uppercase (A-Z) and lowercase (a-z) English letters.
2.Digits (0-9).
3.Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
4.Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.**
</pre>
*Example of valid email id*
<pre>
yoursite@ourearth.com
my.ownsite@ourearth.org
mysite@you.me.net
xxxx@gmail.com
xxxxxx@yahoo.com
</pre>
<pre>
xxxx.ourearth.com [@ is not present]
xxxx@.com.my [ tld (Top Level domain) can not start with dot "." ]
@you.me.net [ No character before @ ]
xxxx123@gmail.b [ ".b" is not a valid tld ]
xxxx@.org.org [ tld can not start with dot "." ]
.xxxx@mysite.org [ an email should not be start with "." ]
xxxxx()*@gmail.com [ here the regular expression only allows character, digit, underscore and dash ]
xxxx..1234@yahoo.com [double dots are not allowed
</pre>
**javascript mail code**
function ValidateEmail(inputText)
{
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(inputText.value.match(mailformat))
{
document.form1.text1.focus();
return true;
}
else
{
alert("You have entered an invalid email address!");
document.form1.text1.focus();
return false;
}
}
这里是一个非常好的讨论,关于使用常规表达式来验证电子邮件地址; “比较电子邮件地址验证常规表达式”
下面是当前的顶级表达式,即JavaScript兼容,用于参考目的:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
推荐文章
- 反应钩子-正确的方式清除超时和间隔
- 使用正则表达式搜索和替换Visual Studio代码
- TypeScript枚举对象数组
- 在React.js中正确的img路径
- 在React.js中更新组件onScroll的样式
- onClick ReactJS调用多个函数
- 如何在JavaScript中转义单引号(')?
- Ng-repeat结束事件
- 谷歌MAP API未捕获的类型错误:无法读取属性“offsetWidth”为空
- 使用split("|")按管道符号拆分Java字符串
- 如何使用HTML/CSS在文本中插入空格/制表符
- 模糊vs聚焦-有什么真正的区别吗?
- 替换字符串中第一次出现的模式
- 如何使用JavaScript创建和样式一个div ?
- “\d”在正则表达式中是数字吗?