14

I want to validate an email with a regex. And the code following shows the regular expression that I've used. The form however does not accept a simple anystring@anystring.anystring. (anystring is an alphabetic).

Am I missing out something?

window.onload = function () {
    document.getElementById("form").onsubmit = validate;
}

function validate() {
    var email = document.getElementsByTagName("input")[6].value;
    var regex = /^\w@\w+\.\w$/;
    if (!regex.test(email)) {
        alert("enter a valid email");
        return false;
    } else return true;
}
faisal bhat
  • 319
  • 3
  • 10
  • You're only allowing a single word character at the beginning and end. You probably wanted `\w+` like in the middle? – Aaron Dufour Sep 17 '15 at 12:49
  • add this /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; – Akhilesh Singh Sep 17 '15 at 12:49
  • 1
    The regex is longer and more complicated. http://stackoverflow.com/questions/46155/validate-email-address-in-javascript –  Sep 17 '15 at 13:02
  • I changed the regex. But it still wont work. Is there anything else that could have gone wrong? – faisal bhat Sep 18 '15 at 03:22
  • Infact not even a simple expression seems to work. Could it be a problem with my browser settings? – faisal bhat Sep 18 '15 at 03:36
  • Do you have JS enabled? Is your validation function even called at all? Are you sure you're targeting the correct input element? Take a look at this fiddle based on your implementation: http://jsfiddle.net/t40afh6k/ – icke Sep 18 '15 at 07:40

2 Answers2

2

Unless you know for sure that email addresses entered will match that format, you should consider using a regular expression that matches more.

"()<>[]:,;@\"!#$%&'*+-/=?^_`{}| ~.a"@üñîçøðé.com is technically(!) a correct email address. While it may not be a good idea to match all of this craziness, this may be what you are looking for and will match the vast majority of email addresses today:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Again, if you are sure your email addresses will be in the format you were going for originally, go with Valinho's answer.

An interesting read about matching email addressses with regular expressions (contains the above regex): http://www.regular-expressions.info/email.html

And the official standard (without unicode madness): https://www.rfc-editor.org/rfc/rfc5322

Community
  • 1
  • 1
icke
  • 1,568
  • 1
  • 19
  • 31
-3

With your currently setted regular expression your are only aceppting one char at the beginning and one char at the end. A valid email for your regex would be for example a@test.a

But you dont want that so change it to: var regex = /^\w+@\w+\.\w{2,3}$/;

You missed the plus char which means that you can enter at least one or more chars. The top level domain usually consists of two or three chars so you can limit it.

Now it should work.

Valinho
  • 105
  • 1
  • 8
  • "The top level domain usually consists of two or three chars" — This is true. That doesn't mean that telling people with unusual email addresses that their email address isn't real is a good idea. – Quentin Sep 08 '16 at 07:48