Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

javascript - Check if an email address is not a "free webmail" (hotmail, yahoo...)

Edit: My Website is offering a service to Gmail and Google Apps users only, and do wanted to make sure that others free email users doesn't get an error message when they initiate the oauth pairing.

so here is the deal : either I try to figure out that's a Gmail/Google Apps address, or try to prevent popular free mail users from trying to subscribe.

I want to prevent all "free" email addresses that belongs to Gmail, Hotmail, Yahoo, etc... from subscribing in my website.

How can I do that correctly in javascript? Here a first script I have developped :

var domain_matche = /@(.*)$/.exec(email);
var domain_name = domain_matche[1].substring(0, domain_matche[1].indexOf(".", 0))
if (domain_name == "hotmail" || domain_name == "yahoo" ) {
      alert("not a valid email");     
} 

But it doesn't detect email like [email protected] or [email protected].

Could you please help? Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all, I would recommend do not do this. As yahoo does offer a Premium PAID service ( I am using that myself and would be annoyed a lot If you did not allow me to register).

Also you would need to implement on both client (JS) and Server (PHP, ASP.net or whatever you are using), as I could easily disable Javascript, and your check would not be executed then.

But if you know what you are doing and want it done properly, Look for first '@' then the following '.' and get the string between them.

Code:

// get index of '@'
var index_at = email.indexOf('@');

// get index of the '.' following the '@' and use it to get the real domain name
var domain = email.substring(index_at + 1,  email.indexOf('.', idx));

// now use the domain to filter the mail providers you do not like

Code to check all sub-domains (for [email protected]) :

// get the string after '@'
var after_at = email.substring(email.indexOf('@') + 1);

// get all parts split on '.', so for [email protected] you can check both x and y
var dot_split = after_at.split('.');
for(var i = 0; i < dot_split.length; i++)
    // check each dot_split[i] here for forbidden domains

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...