我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
我写了一个JavaScript电子邮件验证器,完全兼容PHP的filter_var($值,FILTER_VALIDATE_EMAIL)实施。
https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js
import validateEmail from 'filter-validate-email'
const value = '...'
const result = validateEmail(value)
相当于:
<?php
$value = '...';
$result = (bool)filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE);
其他回答
你不应该使用常规表达式来验证一个输入链来检查它是否是一个电子邮件. 它太复杂,不会覆盖所有案例。
现在,因为你只能覆盖90%的案例,写下这样的东西:
function isPossiblyValidEmail(txt) {
return txt.length > 5 && txt.indexOf('@')>0;
}
例如,‘aaa@’是有效的,但总体而言,你得到了胡萝卜,不要被带走......一个简单的90%的解决方案比一个不起作用的100%的解决方案更好。
世界需要更简单的代码。
有一件事你必须明白,第二件事你决定使用一个常规的表达式来验证电子邮件:这可能不是一个好主意,一旦你达成协议,有很多实施,可以让你在那里半路,这篇文章会把它们合成好。
简而言之,但是,唯一的方式是绝对的,肯定的是,用户输入的是实际上一个电子邮件是实际上发送一个电子邮件,看看发生了什么。
<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;
}
}
\b[a-z][\w\d_\.]+@\w+\.[a-z]{2}[a-z]?\.?[a-z]{,2}\s
它允许:
abcxyz123@qwert.com
abc123xyz@asdf.co.in
abc1_xyz1@gmail1.com
abc.xyz@gmail.com.in
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?