search
Homephp教程PHP源码比较正宗的验证邮箱的正则表达式js代码详解_正则表达式

邮箱正则最正宗的一条正则语句是^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$ 下面我们来讲解一下

fuchangxi的正则:

代码如下:

/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/


开始必须是一个或者多个单词字符或者是-,加上@,然后又是一个或者多个单词字符或者是-。然后是点“.”和单词字符和-的组合,可以有一个或者多个组合。

 代码如下:

<script type="text/javascript"> 
function isEmail(str){ 
var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; 
return reg.test(str); 
} 
var str = &#39;test@hotmail.com&#39;; 
document.write(isEmail(str)+&#39;<br />&#39;); 
var str2 = &#39;test@sima.vip.com&#39;; 
document.write(isEmail(str2)+&#39;<br />&#39;); 
var str3 = &#39;te-st@qq.com.cn&#39;; 
document.write(isEmail(str3)+&#39;<br />&#39;); 
var str4 = &#39;te_st@sima.vip.com&#39;; 
document.write(isEmail(str4)+&#39;<br />&#39;); 
var str5 = &#39;te.._st@sima.vip.com&#39;; 
document.write(isEmail(str5)+&#39;<br />&#39;); 
</script>


我不太了解邮箱的具体规则。感觉这个正则比较简单

统计一下邮箱@前缀的几种类型

1、纯数字
比如:123456@jb51.net
2、纯字母
3、字母数字混合
4、带点的
比如:web.blue@jb51.net
5、带下划线
比如:web_blue@jb51.net
6、带连接线
比如:web-blue@jb51.net
邮箱域至少有一个“.”和两个单词,再严格点那么最后的顶级域至少要2个字母,最大呢?以域名“name”为准,那么最大就是4,宽松点就设为5吧^_^。

当然以上不可能的情况:以“_”或“-”开头或者结尾,包含特殊符号的。
因此,我给出的正则表达式如下:

^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$

代码如下:

<script type="text/javascript"> 
fChkMail=function(szMail){ 
var szReg=/^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/; 
var bChk=szReg.test(szMail); 
return bChk; 
} 
</script> 
<input type="text" id="Mail" value="" /> 
<input type="button" value="验证邮箱地址" onclick="alert(fChkMail(document.getElementById(&#39;Mail&#39;).value));" /> 
<p>邮箱不能以 - _ .以及其它特殊字符开头和结束</p> 
<p>邮箱域名结尾为2~5个字母,比如cn、com、name</p>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor