Reputation: 271
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:
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
}
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
Upvotes: 3
Views: 456
Reputation: 1748
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
}
Upvotes: 3
Reputation: 2670
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:
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:
More on Regex: http://www.regular-expressions.info/reference.html
Upvotes: 2