当前位置:网站首页>Small example of regular expression--validate email address
Small example of regular expression--validate email address
2022-08-05 06:12:00 【CrazyQiQi】
Use regular expressions to verify that the email address is correct
设置一个输入框,一个按钮,A place where information is printed
<label>邮箱:<input type="text" id="email"></label>
<button type="button" id="postEmail">验证邮箱</button>
<p>Print information here</p>
获取输入框、按钮的id,还有p标签节点,Use regular expressions to verify the correctness of the entered email address
//Get the node on which the button was clicked
var post = document.getElementById('postEmail');
post.addEventListener('click', function() {
//给按钮添加2level listener event
//获取p标签节点
var content = document.querySelector('p');
//获取input输入框中的值
var eStr = document.getElementById('email').value;
//正则表达式验证邮箱 例如:
//[email protected]
//[email protected]
//[email protected]
var reg = /^([A-z0-9_-]+)@([A-z0-9_-]+)+(\.([A-z0-9_-]+))+$/g;
if (eStr.match(reg)) {
content.innerHTML = eStr + ' 邮箱正确';
} else {
content.innerHTML = eStr + ' 邮箱有误,请重新输入';
}
})
正确
正确
错误
Let's go into a little more detail about how regular expressions are validated
Two examples of mailboxes
[email protected]
[email protected]
You can see that the email address is basically divided into three parts: [收件人账号]@[邮箱名].[后缀名]
So our regular expression can also be divided into three parts
[Match recipient account(数字/字母//-)]@[邮箱名(数字/字母//-)].[后缀名(数字/字母/_/-)]
var reg = /^([A-z0-9_-]+)@([A-z0-9_-]+)+(\.([A-z0-9_-]+))+$/g;
验证正则表达式
The final track map you can see is like this
边栏推荐
猜你喜欢
随机推荐
什么是阿里云·速成美站?
入门文档03 区分开发与生产环境(生产环境才执行‘热更新’)
入门文档05-2 使用return指示当前任务已完成
正则表达式小实例--验证邮箱地址
Contextual non-local alignment of full-scale representations
入门文档08 条件插件
Unity3D中的ref、out、Params三种参数的使用
入门文档09 独立的watch
[Day1] VMware software installation
markdown编辑器模板
Three modes of vim
D39_欧拉角与四元数
【Day1】(超详细步骤)构建软RAID磁盘阵列
Spark source code-task submission process-6.1-sparkContext initialization-create spark driver side execution environment SparkEnv
Remembering my first CCF-A conference paper | After six rejections, my paper is finally accepted, yay!
D41_缓冲池
[Day8] Commands involved in using LVM to expand
成功的独立开发者应对失败&冒名顶替综
腾讯云消息队列CMQ
Hard Disk Partitioning and Permanent Mounting