0

Check a string contain particular substring or not.

when a user register I want to check entered emailid domain match with my predefined emailid or not(eg @gmail.com ,@outlook.com etc).

1
  • 2
    How about just using indexOf ? Commented May 27, 2014 at 7:28

2 Answers 2

1

Using regular expressions is probably the best way.

function validateEmail(email) { var re = /@gmail.com$/; return re.test(email); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Regular expressions are versatile one you can make custom changes as per requirements
0

You can use indexOf javascript method, or use regular expression:

var mail = "[email protected]"; var domain = "foo"; if(mail.indexOf(domain) > -1) { console.log("domain valid"); } else { console.log("domain not valid"); } 

So you can have your custom function:

/* global domain array */ var domains = new Array("gmail.com", "outlook.com", ...); var checkDomain(mail) { for(var i = 0; i != domains.length; ++i) { if(mail.indexOf(domains[i]) > -1) { return true; } } return false; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.