2

I allow the ability for users to create additional accounts as part of their Company.

IE. Nicky has an account, and her company is Nike. So her email address is nicky@nike.com.

Because her company is 'Nike', I'd only like email addresses she associates with new accounts to be @nike.com. If she tried to create bob@nike.com.au, it wouldn't be allowed.

I'm trying to find the regex that would match Nicky's domain to the one she enters in a new account field. If it's accepted, she can create the new account. Otherwise, it'll throw an error.

Ryan Drake
  • 359
  • 3
  • 17
  • 1
    What is your question? Did you read http://stackoverflow.com/questions/4736/learning-regular-expressions ? – ptd Jun 14 '15 at 23:28
  • 1
    The domain name is a variable your user specifies. To solve this issue, I would get the domain name out of user's specified e-mail using regex and compare it with your variable, not using regex. Then, if it does not match, tell that error has occured and e-mail needs to be redone. – Andris Leduskrasts Jun 14 '15 at 23:42
  • Hi and welcome to Stack Overflow. Here we expect you to have a go at solving the problem yourself and then share with us what you have tried (even if it isn't working). We aren't going to write it for you - or even design it for you. so give it a go and let us know where you get stuck. – Taryn East Jun 15 '15 at 02:15
  • I don't really see a use case for a regexp, since you can just get the substring starting with @ and check for ends-with type of thing. A lot less error-prone and performant. – Sami Kuhmonen Jun 15 '15 at 07:04

1 Answers1

4

Are you familiar with regex? You shouldn't come and say "I need a code that does this: ..." without giving your own input, your post can come off as rude. I'll answer your question about the usage of regex though.

You can get the domain out of your e-mail input with a regex like this: (?<=@)([^\s]+)(?=\s|$) (doesn't work in javascript due to lookbehind). See https://regex101.com/r/qX1qF5/3 for examples and description.

Based on some answers on stackoverflow, if you're using Ruby, which we can only assume you are because of tags and not your actual post, you can capture it using scan or match:

domain = eMailInput.scan(/(?<=@)([^\s]+)(?=\s|$)/).first

Then you compare it to your domain name variable and you get the answer you need. It doesn't seem like the actual "validation" part should be done with regex.

That's the theory, anyway, and the way how I would approach this problem. Now go ahead and use it in your code and tell us if it works or if there are problems you're encountering.

Andris Leduskrasts
  • 1,170
  • 6
  • 16