我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
<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;
}
}
其他回答
有一件事你必须明白,第二件事你决定使用一个常规的表达式来验证电子邮件:这可能不是一个好主意,一旦你达成协议,有很多实施,可以让你在那里半路,这篇文章会把它们合成好。
简而言之,但是,唯一的方式是绝对的,肯定的是,用户输入的是实际上一个电子邮件是实际上发送一个电子邮件,看看发生了什么。
做这:
^([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)$
基于 RFC 2822
点击 https://regex101.com/r/857lzc/1
经常在数据库中存储电子邮件地址时,我会使它们变得更低,在实践中, regexs 通常可以被标记为案例不敏感。
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
下面是它在JavaScript中使用的一个例子(最终是无敏感的旗帜)。
var emailCheck=/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
console.log( emailCheck.test('some.body@domain.co.uk') );
注意: 技术上,一些电子邮件可以包含引用在 @ 符号之前的部分与逃避字符在引用中(因此,您的电子邮件用户可以是无情的,并包含如 @ 和“......”等内容,只要它是写在引用中)。
注意2:电子邮件的开始(在 @ 标志之前)可能是案例敏感(通过规格)。但是,任何具有案例敏感电子邮件的人可能会遇到问题,在实践中,案例不敏感是一个安全的假设。
在 nodeJS 中,您也可以使用验证器节点模块,并简单地使用此类
使用 npm 安装验证器安装图书馆
var validator = require('validator');
validator.isEmail('foo@bar.com'); //=> true
JavaScript 可以匹配一个常规表达式:
emailAddress.match( / some_regex /);
下面是RFC22电子邮件的常规表达式:
^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*
"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x
7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<
!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])
[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$
我真的很高兴能解决这个问题,所以我修改了电子邮件验证的常规表达。
<unk> <unk> <unk> <unk> <unk> <unk> <unk> <unk>
在维基百科的电子邮件地址中提交例子。
你可以在这里看到结果。
此分類上一篇
推荐文章
- 转换php数组到Javascript
- 如何跳转到浏览器页面的顶部
- 在电子邮件消息中是否支持JavaScript ?
- 如何在JavaScript中指定Math.log()的基?
- 多重左赋值JavaScript
- 如何在单个测试基础上更改模拟实现?
- VueJS有条件地为元素添加属性
- Uncaught TypeError:(中间值)(…)不是一个函数
- 如何设置URL查询参数在Vue与Vue路由器
- 无法读取属性“addEventListener”为空
- 如何防止moment.js从webpack加载地区?
- getMonth在javascript中给出前一个月
- 两行文本溢出省略号
- 如何在禁用按钮上启用引导工具提示?
- Node.js全局变量