2

I want to validate email address in html form. I have very little knowledge in regex.

Validation is very simple, just to match any_symbols@any_symbols.two_to_eight_symbols pattern.

Here is the regex I'm trying to use ^.+@.+\..{2,8}$. Yet it doesn't work, it validates pattern any_symbols@four_symbols.

Note: do not worry about such simple validation, on server side I'm doing filter_var (php) and sending token to that email. Just need to enable button on form when inputed email address fits some sane pattern :)

EDIT Those patterns "any_symbols..." I've mentioned in question are just textual representation of what I'm trying to input. This is not what I type in input field :) Usually I type "test@test.com", or "blabla@hehe.haha" and etc. :)

EDIT2 Actuall code:

   var email_regex = new RegExp("^.+@.+\..{2,8}$");
if ($target.val().match(email_regex) !== null){
    $button.removeAttr('disabled').removeClass('disabled');
}
else{
    $button.attr('disabled', 'disabled').addClass('disabled');
}

EDIT3 *Found the problem!* It wasn't the regex itself, it was how I passed regex to Regex function... it should be

 new RegExp(/^.+@.+\..{2,8}$/);

not the

new RegExp("^.+@.+\..{2,8}$");

As I've said this whole regex thing is quite new to me :))

egis
  • 1,404
  • 2
  • 11
  • 24
  • 1
    In my browser, `/^.+@.+\..{2,8}$/.test("any_symbols@four_symbols")` is `false` :-? – Álvaro González Apr 25 '12 at 14:40
  • Try `'any_symbols@four_symbols'.match('^.+@.+\..{2,8}$');` – egis Apr 25 '12 at 14:48
  • Updated my question. Also according to https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match "If there were no matches, the method returns `null`.". – egis Apr 25 '12 at 14:56
  • 1
    `RegExp(/foo/)` is redundant. The constructor expects a string, thus `/foo/` gets probably casted to `"foo"`. It fixes the problem by pure chance. – Álvaro González Apr 25 '12 at 15:09
  • So why does `RegExp(/foo/)` does the trick and `RegExp("foo")` doesn't? – egis Apr 25 '12 at 15:11
  • 1
    Because the `\.` in `/\./` is read as `\.`, while the `\.` in `"\."` is read as `.`. The JavaScript parser does not follow the same rules in regexp and string literals. – Álvaro González Apr 25 '12 at 15:13

3 Answers3

1

Not a Javascript expert, but with your definition of any_symbols@any_symbols.two_to_eight_symbols I think this could work:

^[^@]+@[^.]+\..{2,8}$
Birei
  • 35,723
  • 2
  • 77
  • 82
  • it's different than mine regexp, but still matches the same pattern. I mean it also matches `any_symbols@four_symbols` instead of `any_symbols@any_symbos.2_to_8_symbols` – egis Apr 25 '12 at 14:51
  • Sorry, your regex was correct, but I hope you don't mind that I'll accept answer of lu1s, because he has not so much reputation score as you do :) Thank you! – egis Apr 25 '12 at 15:03
1

Suposely the regex for a real email address is not quite simple, since it should comply with an RFC standard.

However, if this works for you, I've used it with no problems:

// The regex: /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/

// To test it:
var regex = new RegExp(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/);

if( regex.test( your_string ) ){ ... } // returns true if valid

If you really need to accept ANY symbols, you can use the regex that Birei posted.

lu1s
  • 5,600
  • 3
  • 22
  • 37
  • I found the problem myself and it wasn't regex, but I'm accepting your answer, since you gave informative answer and to boost your reputation score ;) Thank you! – egis Apr 25 '12 at 15:04
  • I preferred the original expression. At least, it would accept internationalized domain names (IDN). – Álvaro González Apr 25 '12 at 15:11
  • Álvaro, you're right. This one would not accept japanese or any other symbols. I've never thought about it. Thanks for this. Maybe this thread'll work: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – lu1s Apr 25 '12 at 18:36
1

You need to escape the slash:

'any_symbols@four_symbols'.match('^.+@.+\\..{2,8}$');
                                        ^^

Otherwise, JavaScript reads \. as an unknown escape sequence and ignores the slash:

For characters not listed [...] a preceding backslash is ignored, but this usage is deprecated and should be avoided.

(Damn, this was hard to spot...)

Álvaro González
  • 142,137
  • 41
  • 261
  • 360