views:

60

answers:

2

Hello all,

I think many people have done some similar development tasks before:

I would like to check the people's email address whether only match @tomtom.com or @stream.com.

Currently, I have two solutions in my mind:

  1. Using indexof() function

    var checkTomTomEmail=eo.data.username.indexOf("@tomtom.com");
    var checkStreamEmail=eo.data.username.indexOf("@stream.com");
    
    
    if (checkTomTomEmail >0 || checkStreamEmail >0 )
    {
        //Run the login code
    }
    
    
    Else 
    {
        //Please login with your tomtom or stream email 
    }
    
  2. Using match

    var patt1=/@tomtom.com/gi;
    var patt2=/@stream.com/gi;
    var checkTomTomEmail=eo.data.username.match(patt1);
    var checkStreamEmail=eo.data.username.match(patt2);
    
    
    if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1)
    {
        //Login 
    }
    

I still think I do not consider all the detail yet. Any suggestions?
Thanks

+2  A: 

Perhaps if people are only allowed to enter emails for those two addresses you should only collect the username and then allow them to choose @tomtom.com or @stream.com using radiobuttons.

If you still want to go the javascript route then your regex can be combined into a single statement

var emailPatt=/@(tomtom|stream).com/gi;

if(emailPatt.test(eo.data.username))
{
    //Login 
}
Evil Andy
that will too much customziation in our vendor system. THansk Evil Anday, what do u think the match solution?
QLiu
+2  A: 

How about this...

var emailRegex = /^([0-9a-z])+@(tomtom|stream)\.com$/ig;
if (emailRegex.test(emailRegex)) {
    // Login
}

Instead of performing a .match(...) - Which you'll get a string back, we can perform a .test(...) to see if anything matches.

This pattern guarantees the following:

  1. The "username" part of the email address must at least have a SINGLE character (For example, [email protected])
  2. Username must be composed of a digit or an alphabet (Upper/Lower case - Doesn't matter because of the /i at the end)
  3. Input must contain the entire email address without leading or tailing spaces. For example, " [email protected] " will fail, it'll only accept "[email protected]".)

You can customize this further by, saying, making sure username must have at least 3 characters, you can use underscore or dashes in the email address, etc.

To answer your question, both solutions won't work. Reasons:

  1. User can enter "[email protected] Hello", and it'll pass both of your validation.
  2. Specifically on solution #2, the dot '.' is a Regex-reserved character, it means it'll match anything, so, if the user enters " @tomtom1com", it'll pass...

More on Regex: http://www.regular-expressions.info/reference.html

DashK