我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
如果您想要使用 JQuery 并想要具有现代化的方法,则使用 JQuery 输入面具与验证。
http://bseth99.github.io/项目/jquery-ui/5-jquery-masks.html
关于如何简单的jQuery输入面具的演示文稿在这里: http://codepen.io/anon/pen/gpRyBp
一个简单的输入面具的例子为日期 forexample 没有完整的验证
<input id="date" type="text" placeholder="YYYY-MM-DD"/>
还有剧本:
$("#date").mask("9999-99-99",{placeholder:"YYYY-MM-DD"});
其他回答
<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;
}
}
<input type="email" class="form-control" required="required" placeholder="Email Address" name="Email" id="Email" autocomplete="Email">
<button class="btn-1 shadow-0 full-width" type="button" id="register">Register account</button>
$("#register").click(function(){
var rea = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var Email = $("#Email").val();
var x = rea.test(Email);
if (!x) {
alert('Type Your valid Email');
return false;
}
</script>
做这:
^([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:电子邮件的开始(在 @ 标志之前)可能是案例敏感(通过规格)。但是,任何具有案例敏感电子邮件的人可能会遇到问题,在实践中,案例不敏感是一个安全的假设。
您可以使用此 regex (从 w3 资源(与 W3C 无关):
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailValue)
如果您使用 Node,您可以在后端以及前端使用此功能。
我不知道其他背景语言,所以我不能对其他使用情况进行评估。
我正在寻找一个 Regex 在 JS 通过所有电子邮件地址测试案例:
email@example.com 有效电子邮件 firstname.lastname@example.com 电子邮件包含地址字段 email@subdomain.example.com 电子邮件包含字段与域名 firstname+lastname@example.com Plus 标志被认为是有效的字符 email@192.0.2.123 域名是有效的 IP 地址 email@[192.0.2.123] IP 地址周围的广场 被认为是有效的“email”@example.com 电子邮件周围的引用是 c
这里我们去:
HTTP://regexr.com/3f07j
或雷格斯:
Regex = /(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@[*[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+]*/
推荐文章
- 转换php数组到Javascript
- 如何跳转到浏览器页面的顶部
- 在电子邮件消息中是否支持JavaScript ?
- 如何在JavaScript中指定Math.log()的基?
- 多重左赋值JavaScript
- 如何在单个测试基础上更改模拟实现?
- VueJS有条件地为元素添加属性
- Uncaught TypeError:(中间值)(…)不是一个函数
- 如何设置URL查询参数在Vue与Vue路由器
- 无法读取属性“addEventListener”为空
- 如何防止moment.js从webpack加载地区?
- getMonth在javascript中给出前一个月
- 两行文本溢出省略号
- 如何在禁用按钮上启用引导工具提示?
- Node.js全局变量