-1

I am using a regular expression to validate email addresses in an online form. A lot of emails are entered as such: www.test@example.com

How can I disallow the use of www. using regex? My current expression:

^[\+_a-z0-9-'&=]+(\.[\+_a-z0-9-']+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$

I know this expression checks for a gTLD - we require an email address to have one.

EDIT: This question is slightly different to others I have found as I wasn't sure how to add the www exclusion in - that's all I needed.

Also, the target market we reach doesn't have the same experience using technology as most people do, so the amount of email addresses that we receive with www in them is massive. www.john@gmail.com is perfectly valid yes but it is 99% of the time not what the persons email address is. Our CRM system can't send emails to the correct people then.

GiarcTNA
  • 499
  • 2
  • 17
  • 1
    [Please don't use a regex to validate an email](https://stackoverflow.com/q/46155/4519059). If you have a specific situation let us know it by adding some examples and results ;). – shA.t Aug 24 '17 at 06:55
  • 1
    BTW, You can use a negative look forward like `^(?!www\.)...` ;). – shA.t Aug 24 '17 at 06:58
  • I am aware of the negatives of using a regex, however the addresses we are validating are very simple address like `test@example.com`. We need the gTLD as people often forget them. Having said that, we do not need just domain addresses such as `test@example`, while it is valid, our target market does not use them. In the odd case one or two might it will be a far less amount that the number of people who forget a .com for example. – GiarcTNA Aug 24 '17 at 07:26
  • So a regex like [`^\w+@\w+\.[a-z]{2,}$`](https://regex101.com/r/y7tnTN/1) will fit your needs ;). – shA.t Aug 24 '17 at 07:49
  • Why do you want to do such thing? `www.test@example.com` is a perfectly valid email. – Toto Aug 24 '17 at 10:12

1 Answers1

1

you can use the regex

^(?!www\.)[\+_a-z0-9-'&=]+(\.[\+_a-z0-9-']+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$

see the regex demo

marvel308
  • 10,288
  • 1
  • 21
  • 32