views:

73

answers:

4

I want to do this but dont know how to do it in JavaScript.

if (Email == "*aol.com" || Email == "*hotmail*" || Email == "*gmail*" || Email == "*yahoo*") 
    {
     alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");  
     return false;
    }

Any way around this?

+3  A: 

Use the JavaScript REGEXP Object. Read the W3schools page or their tutorial for a few examples. If you have problems with either, just post those here and we'll get into more detail :)

var Email = '[email protected]';

var mail_pattern=new RegExp("/aol.com|hotmail|gmail|yahoo/");
if (mail_pattern.test(Email)) {
 alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");  
}
else {
alert("mail ok")
}
Konerak
How? Could you perhaps include an example, or a link to a tutorial?
Dominic Rodger
Can you please give me an example? Dont know what that is......
Etienne
+6  A: 

First, I think your searches are a bit too generic. (e.g. what if someone has the email address, "[email protected]"?

Try this:

var notAllowed = /@(?:aol|hotmail|g(?:oogle)?mail|yahoo)\.com$/i;
// You may want to cover other domains under .co.uk etc.

if ( notAllowed.test(emailAddress) ) {
    alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");  
    return false;
}

I have to ask, why are you disabling people from using these email addresses? (out of interest)

Also, this really should be done on the server-side (I'm assuming you're not using SSJS)

J-P
+1 for every sentence!
deceze
Management want to see when competitors are filling out form to see our documents.
Etienne
Cant be server side because they are plain HTML pages.
Etienne
If you can't do it serverside, atleast know that everyone can edit your javascript and just remove the test or change the return false into return true... clientside only is not secure.
Konerak
MMM, true, lets just hope they are not that clever!
Etienne
@Etienne, always assume you have users that are both smarter and stupider than you.
Zano
Etienne: they will be. Or they could just have a browser with javascript disabled. Or ... or ... or ...
Konerak
I think you need to educate your management that its' better to gather and interpret statistics than annoy your users by not allowing them to use their e.g. personal email address.
JBRWilkinson
A: 

Best way is the REGEXP. Quick easy thing is indexOf method maybe, but it's so limited.

http://www.quirksmode.org/js/strings.html#indexof

Bashar
A: 

Just for messin' with the people suggesting regexes, you could also do it programatically:

(I'm kinda suggesting you don't know JavaScript that well)

function validateEmail (email) {
    // Checks if whatever variable submitted is a string.
    // This line is NOT useful if you're sure of whatever being sent to the function,
    // but otherwise it'll save your ass for a runtime error.
    if (email.constructor != String) return false;

    // I'm not sure the search patterns are 100 % (they probably ain't),
    // but the @'s should make the search less generic.
    var emailsNotAllowed = ["@aol.com", "@hotmail.", "@live.", "@gmail.", "@google.", "@yahoo."];
    // Convert the email address to lower case, so you're sure nothing is going wrong with the patterns.
    email = email.toLowerCase();
    // Loop through all the patterns, and check if one of them matches the email address provided.
    for (var i = 0; i < emailNotAllowed.length; i++) {
        // The indexOf method will return zero if the string could not be found.
        // Otherwise, it will return a positive number, which in JavaScript validates to a true condition.
        if (email.indexOf(emailsNotAllowed[i])) {
            alert("No Hotmail, Gmail, Yahoo or AOL emails are allowed!");
            return false;
        }
    }
}

Hope it explains itself ;)

As also suggested, a check should be performed server side as an absolute minimum, otherwise you'll have no security. But, for usability, a client side check first is also good.

Sune Rasmussen