-1

In addition to serverside validation I want to have some clientside validation (javascript/jquery) for an email input field accepting only emails where its address is at least 3 characters long and its tld at least 2 chararacters.

As of right now I'm checking the whole Email like this:

html:

<input type="email" name="email" placeholder="E-Mail" class="form-control" id="chatemail">

javascript:

var re_email = new RegExp(/^[a-zA-Z0-9\_\.\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z]+)$/);
var is_email = re_email.test($('#chatemail').val());

// and its length like this:
$('#chatemail').val().length > 5

But in addition how can I check that the email ending e.g. @gmx.de

... that the 'gmx' is at least 3 digits long and the the 'de' is at least 2 digits long? So what I don't want is that somebody can type in @g.d or @gm.c ... and stuff like that ...

I tried to split the regex ... but my attempts doesn't work ...

var re_email_01 = /^[a-zA-Z0-9\_\.\-]$/;
var re_email_02 = /^@[a-zA-Z0-9\-]$/;
var re_email_03 = /^(?:\.[a-zA-Z]+)$/;

var re_email = new RegExp(re_email_01+'+'+re_email_02+'+'+re_email_03);

I also tried this ...

var re_email = new RegExp(/^[a-zA-Z0-9\_\.\-]+@[a-zA-Z0-9\-]{3,}+(?:\.[a-zA-Z]{2,}+)$/);
Philipp M
  • 2,534
  • 3
  • 19
  • 62
  • When attatching {3,} and {2,} like this ... var re_email = new RegExp(/^[a-zA-Z0-9\_\.\-]+@[a-zA-Z0-9\-]{3,}+(?:\.[a-zA-Z]{2,}+)$/); ... I get an SyntaxError: nothing to repeat ... where did I put +s? – Philipp M May 02 '17 at 11:17

3 Answers3

1

Your e-mail regular expression is very restrictive. Answering the question, you may re-write your pattern to accommodate for your requirement like this:

var re_email = /^[-.\w]+@(?![^.]{0,2}\.[a-zA-Z]{2,}$)([-a-zA-Z0-9]+\.)+[a-zA-Z]{2,}$/;

See the regex demo

You should not add + after the limiting quantifiers ({min,max} constructs), JS regex does not support possessive quantifiers like Java or PCRE, it is an invalid construct. Also, no need to use new RegExp with a static pattern, just use a regex literal.

An important part here is the (?![^.]{0,2}\.[a-zA-Z]{2,}$) negative lookahead: it fails the match if the domain part is only 0 to 2 chars other than a dot and then comes a dot and 2 or more letters.

Note also that [a-zA-Z0-9_] matches the same as \w in JS regex and when you put the - at the start of a character class, you do not need to escape it to be parsed as a literal hyphen.

Pattern details:

  • ^ - start of string
  • [-.\w]+ - 1 or more -, . or alphanumeric/_ symbols
  • @ - a @ symbol
  • (?![^.]{0,2}\.[a-zA-Z]{2,}$) - there cannot be 0 to 2 chars other than . followed with . and then 2 or more letters up to the end of the string immediately to the right of @
  • ([-a-zA-Z0-9]+\.)+ - 1 or more sequences of - or alphanumeric chars followed with a dot
  • [a-zA-Z]{2,} - 2 or more ASCII letters
  • $ - end of string.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

i use the html5 build in function for e-mail validation:

function emailValid(emailAddy){
  var email = document.createElement('input');
  email.type = 'email';
  email.value = emailAddy;
  return email.validity.valid; 
};

alert (emailValid('ttt@gmx.de'));
alert (emailValid('tt t@gm x.de'));

js fiddle

Frank Wisniewski
  • 1,074
  • 6
  • 6
0

You can use the below regex for your requirements:

/^[.-\w]+@[\w\-]{3,}((.)\w{2,})+$/

This allows letters, digits and (-, _, .) in the first part of the email address and ensures that atleast 3 characters are there after '@' and then atleast 2 after the dot(.). This regex also takes care of ensuring subsequent dot(.)'s also have 2 characters after them like you've mentioned in the rules. Samples:

// Matching
abc@gmx.dcom
newemail@gmail.com
8-d_873@abc.co
abc.deg@new.co
abc@9mail.co
d@mail.com
abc.deg@new.co.uk

// Not matching
abc@gmc.d
abc@gm.de
d@ma.com
d@
9@99.213
first.name@gmx...de
RBT
  • 452
  • 2
  • 15
degant
  • 4,466
  • 1
  • 13
  • 28
  • I like ... thanks a lot! – Philipp M May 02 '17 at 12:09
  • @PhilippM: Do you want to allow input like [`e@eee...`](https://regex101.com/r/ekqfcr/1)? The solution you accepted allows these kinds of inputs. – Wiktor Stribiżew May 02 '17 at 12:16
  • @Wiktor Stribiżew: Good point ... although ensuring subsequent dot(.)'s would be nice ... I wouldn't like them to provide sth. like first.name@gmx...de . How can both be assured? – Philipp M May 02 '17 at 12:36
  • Great point @WiktorStribiżew. I've modified the regex to not match with consecutive dots. Take a look at the updated regex and refiddle demo. Thanks! – degant May 02 '17 at 12:42
  • @degant: nice ... and here comes the 'accept' again :-) – Philipp M May 02 '17 at 12:49
  • @PhilippM I doubt you want to match [`e@---/sc/cccc/cccc/cccc/ccc`](https://regex101.com/r/ekqfcr/2). See my solution. – Wiktor Stribiżew May 02 '17 at 12:53
  • Glad I could help. However there are still a few problems which can be fixed easily (like the email could start with a number or an underscore etc.) – degant May 02 '17 at 12:53
  • @degant: Starting with a number is fine I think ... but to make sure its not starting with an underscore ... I added ... /^[a-zA-Z0-9]+[.-\w]+@[\w\-]{3,}((.)\w{2,})+$/; – Philipp M May 02 '17 at 13:11
  • @WiktorStribiżew: For sure not. I can see on regex101 that e@---/sc/cccc/cccc/cccc/ccc is working ... yet ... not on my live environment ... – Philipp M May 02 '17 at 13:19