简体   繁体   English

Email 正则表达式验证 javascript

[英]Email regex validation javascript

I do understand the problems with validating emails but I wonder whether this would block anyone that has a legal email.我确实理解验证电子邮件的问题,但我想知道这是否会阻止任何拥有合法 email 的人。

I was looking for a list of valid emails to test it myself but did not find any.我正在寻找一个有效电子邮件列表来自己测试它,但没有找到。

Anyone have an email that is valid but this regex thinks it's not?任何人都有一个有效的 email 但这个正则表达式认为它不是?

emailRegex.test('Emailing@domain.aero')

Very long line:很长的一行:

emailRegex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.(([a-z]{2}|AERO|ARPA|ASIA|BIZ|CAT|COM|COOP|EDU|GOV|INFO|INT|JOBS|MIL|MOBI|MUSEUM|NAME|NET|ORG|PRO|TEL|TRAVEL|XN--0ZWM56D|XN--11B5BS3A9AJ6G|XN--3E0B707E|XN--45BRJ9C|XN--80AKHBYKNJ4F|XN--90A3AC|XN--9T4B11YI5A|XN--CLCHC0EA0B2G2A9GCD|XN--DEBA0AD|XN--FIQS8S|XN--FIQZ9S|XN--FPCRJ9C3D|XN--FZC2C9E2C|XN--G6W251D|XN--GECRJ9C|XN--H2BRJ9C|XN--HGBK6AJ7F53BBA|XN--HLCJ6AYA9ESC7A|XN--J6W193G|XN--JXALPDLP|XN--KGBECHTV|XN--KPRW13D|XN--KPRY57D|XN--LGBBAT1AD8J|XN--MGBAAM7A8H|XN--MGBAYH7GPA|XN--MGBBH1A71E|XN--MGBC0A9AZCG|XN--MGBERP4A5D4AR|XN--O3CW4H|XN--OGBPF8FL|XN--P1AI|XN--PGBS0DH|XN--S9BRJ9C|XN--WGBH1C|XN--WGBL6A|XN--XKC2AL3HYE2A|XN--XKC2DL3A5EE0H|XN--YFRO4I67O|XN--YGBI2AMMX|XN--ZCKZAH|XXX)(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)|(^$)/i;

Dominic Sayers has created a list of email edge cases that you could use to validate your test. Dominic Sayers创建了一个 email 边缘案例列表,您可以使用这些案例来验证您的测试。 You can find it here .你可以在这里找到它。

The valid address test@[IPv6:::] , "test\ test"@iana.org or "test@io" are not accepted by your regex.您的正则表达式不接受有效地址test@[IPv6:::]"test\ test"@iana.org"test@io"

Here is the code for html input field and button field这是 html 输入字段和按钮字段的代码

<input input type="text" name="txtEmailId" id="txtEmailId" /> 
   <input type="submit" class="button" value="Suscribe" name="Suscribe" 
            onclick="javascript:ShowAlert()" />

Now add the below function to the header of your page现在将以下 function 添加到您页面的 header

<script type="text/javascript">
 function ShowAlert() {
  var email = document.getElementById('txtEmailId');
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        alert('Please provide a valid email address');
        email.focus;
        return false;
    }
    else {
        alert("Thanks for your intrest in us, Now you 
        will be able to receive monthly updates from us.");
        document.getElementById('txtEmailId').value = "";
    }
 }
 </script> 

Here you can find the article on this Email Validation in JavaScript在这里,您可以在 JavaScript 中找到有关此 Email 验证的文章

It's a beautiful expression, but soon to be relegated to the realm of obsolescence:这是一个美丽的表达,但很快就会被降级为过时的 realm:

http://www.icann.org/en/topics/new-gtld-program.htm http://www.icann.org/en/topics/new-gtld-program.htm

Global Top Level Domains (yourbestfriend@worksfor.coke) are coming, and they'll break all of our scripts in a few years:)全球顶级域名 (yourbestfriend@worksfor.coke) 即将到来,它们将在几年内破坏我们所有的脚本:)

Though, to answer your question, no, I was not able to break your email check using today's finite limit on "valid" domain extensions.但是,要回答您的问题,不,我无法使用今天对“有效”域扩展的有限限制来打破您的 email 检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM