-1

I have this regex

var reg = /^[a-zA-Z0-9._]+@[gmail | yahoo | hotmail]{5,7}.[com | net | org]{3}$/

I want to accept just "gmail hotmail and yahoo" with ".com .net and .org" Is there any method...to set the above regex according to my needs.

Also how to set the range so that it only accept the length of 5 and 7 characters.

  • `(gmail|yahoo|hotmail)` but... why? And if you're specifying strings to match, you don't want any quantifier on that (unless you're looking to match `"gmailgmailgmailgmailgmail"` – Niet the Dark Absol Apr 06 '19 at 09:58
  • Use an alternation with a `|` instead of using that in the character class `^[\w.]+@(?:gmail|yahoo|hotmail)\.(?:com|net|org)$` – The fourth bird Apr 06 '19 at 09:59
  • Using a regular expression to check a mail address is useless, unless you're using the [complete expression](http://ex-parrot.com/~pdw/Mail-RFC822-Address.html). Your example would allow `johndoe@gmail.com` but not `johndoe+alias@gmail.com` although they both end in the same mailbox. – Andreas Apr 06 '19 at 10:05
  • 1
    @The fourth bird thanks men it work for me – Muhammad Hassan Apr 06 '19 at 10:11
  • what do you mean by `length of 5 and 7 characters`? – Dacre Denny Apr 06 '19 at 10:12
  • @Andreas also, if it allows for the domain name and the TLD *separately*, you could put in `gmail.net` – VLAZ Apr 06 '19 at 10:27
  • how do i validate that email must start with alphabet...not with number – Muhammad Hassan Apr 06 '19 at 16:49

1 Answers1

0

The correct syntax for this looks like this:

^[a-zA-Z0-9._]+@(gmail|yahoo|hotmail)\.(com|net|org)$

Explanation

Squared brackets ([]) are used to test for testing a character in a group of given characters. Round brackets (()) are used to test for a particular string (not character) in a group of given strings. You should read the | as "or".

Examples

  • [abc] will match a or b or c.
  • (A1|B2|C2) will match A1, B2 or C3.

Additionally, the . character will be interpreted as "any character" if you don't escape it (\.), except when being using inside squared brackets.

Thomas Dondorf
  • 23,416
  • 6
  • 84
  • 105
  • That;s a correct *syntax* but I'm not sure that's a correct regex. Leaving aside the validity of the name, this allows `gmail.net` which is not a correct email domain. – VLAZ Apr 06 '19 at 10:28
  • 1
    As a separate note "*Additionally, the `.` character will be interpreted as "any character" if you don't escape it (`\.`).*" this is not true if it's inside character class. `[.]` will never be interpreted as anything but a literal `.` https://stackoverflow.com/questions/399078/what-special-characters-must-be-escaped-in-regular-expressions – VLAZ Apr 06 '19 at 10:30
  • 1
    @VLAZ The regex is doing what OP asked for, so I consider it correct. Might be a case of a [XY problem](https://meta.stackexchange.com/a/66378). Thanks for your information regarding the `.`, I added that information to the answer. – Thomas Dondorf Apr 06 '19 at 10:41
  • how do i validate that email must start with alphabet...not with number. – Muhammad Hassan Apr 06 '19 at 16:49
  • @MuhammadHassan Just put in another block in the beginning like this: `^[a-zA-Z][a-zA-Z0-9._]+...`. – Thomas Dondorf Apr 06 '19 at 16:51
  • @MuhammadHassan If my answer solved your problem, you can accept it as answer by clicking the big checkbox next to the answer. – Thomas Dondorf Apr 06 '19 at 16:53
  • it help me but i don't know where it is to select the best answer... thanks – Muhammad Hassan Apr 06 '19 at 17:11
  • @MuhammadHassan You can find an explanation [here](https://meta.stackexchange.com/a/5235) :) – Thomas Dondorf Apr 06 '19 at 17:14