0

I am using a regex to validate an email address in JavaScript.

The regex is pretty simple. It checks three things: 1)'@' , 2)'.' ('dot' as in [email protected]), and 3) 'a-z' in an email address. If all three return true, email address is valid (according to my validation, atleast)

Here is the code:

function checkemail(){
  var e = document.getElementById("email").value;
  if((e.match(/@/g)==null)||(e.match(/[a-z]/ig)==null)||(e.match(/./g)==null)){
  //display error message
  }
}

My question is:

(e.match(/./g)==null); //returns false even though there are no dots in the string e

returns false even when there are no dots in string.

For example:

("thisIsMyEmail".match(/./ig))==null //returns false

Why does it return false when it should be true?

3
  • 1
    periods are special in a regex, it means "any character", not just a period, to make it mean a period, it has to be escaped. Commented Dec 6, 2014 at 17:03
  • Related: stackoverflow.com/questions/46155/… Commented Dec 6, 2014 at 17:05
  • Instead of using match for literal strings, you should use String.prototype.indexOf() Commented Dec 6, 2014 at 17:08

5 Answers 5

5

/./g (or /./ig) will match any string that as at least one character in it. . is special in regular expressions, it means "any character here."

For an actual dot, escape it with a backslash: /\./g.

Sign up to request clarification or add additional context in comments.

Comments

2

First off, you don't need to check if the string is null. Simply use this:

var email = "[email protected]";

if (email.match(/^\S+\@\S+\.\S+$/i)){
alert("Email address passed validation.");
}

2 Comments

Thanks for your response. It does pretty much the same as mine. It does not seem to check for letters after the final dot. Eg: bob@gmail. returns true. But it helps. Thanks
@raneshu, I'm sorry, my code was a bit messy. Try it now.
1

you have to escape the . The unescaped period means matches any character.

Meaning having a string "abc" using your expression would result in an array containing the characters 'a', 'b', and 'c'.

In your snippet the correct answer is

(e.match(/\./g)==null);

This should result to what you're expecting

Comments

1

Try this

(e.match(/\./g)==null);

. matches any character so needs escaping /\./g

Comments

0

I know you have already got the answer.

But I just want to give an advice. My advice is - don't use the javascript code to validate any email address; because as per your code, @domain., @domain.com these all are also valid email, but everybody knows these are not a valid email address.

So use the below code:

let email = $(this).val();
var positionOfAt = email.indexOf("@");
var positionOfDot = email.lastIndexOf(".");

if(email.search("@") == -1 || //if '@' is not present
email.search(" ") >= 1 || //if blank space is present
email.search(".") == -1 || //if "." is not present
positionOfAt < 1 || //if there is no character before "@", at least one character should be present before "@"
positionOfDot - positionOfAt <= 2 || //between '@' and '.', if there is not at least two character
email.length - positionOfDot <= 2) //if after '.' there is not at least two character)
{
    console.log("Invalid email id")
}
else
{
    console.log("Valid email id")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.