简体   繁体   English

使用Java自定义电子邮件验证

[英]Customized email Validation in Javascript

I am using Javascript email validation using regular expression. 我正在使用正则表达式使用Javascript电子邮件验证。

I am using the below function 我正在使用以下功能

function validateEmail(email) { 
     var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

It works fine for various email validation scenario. 它适用于各种电子邮件验证方案。 Let say for example 比方说

validateEmail('m@s'); // return false

But if I try to validate something like 但是如果我尝试验证类似

validateEmail('m@com.com'); // return true

Which is true as per the regular expression. 根据正则表达式,这是正确的。

What I need is, it will also validate that text "com" before and after ".", such a that both the texts should not be the same. 我需要的是,它还将验证文本“。”之前和之后的“ .com”,这样两个文本不应相同。

Any help will be appreciated... :) 任何帮助将不胜感激... :)

I have done some cosmetic changes in above code and it works.. :) 我在上面的代码中做了一些外观上的更改,并且可以正常工作.. :)

Below is the changed function in which valid email is rechecked for the text after "@" but before and after the "." 以下是更改后的功能,其中重新检查了有效电子邮件,以检查“ @”之后但之前和之后的文本。

function validateEmail(email) { 
   var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;  
     if(re.test(email)){   
       var afteratRate = email.substr(email.indexOf("@") + 1); 
       var spmail = afteratRate.split(".");
       if(spmail[0] === spmail[1]){
           return false;
        }
    }
    return re.test(email);
}

Then test for below scenarios 然后测试以下情况

validateEmail('m@.com'); // gives false
validateEmail('m@s.com'); // gives true
validateEmail('test@gmail.com'); // gives true
validateEmail('test@com.com'); // gives false

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

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