Jump to content

email validation


dalawh

Recommended Posts

I was looking through some ways to validate email. I saw the one on w3schools, but that is a basic one, so I started to code something more complicated adding all those if, else if, else statements, etc. Then I came across this...

function validateEmail(email){var allowed=/^([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$)/; return allowed.test(email);}

Is something that simple sufficient enough? Everything seems to look as if it would work. Also, what does the "$" do?

Link to comment
Share on other sites

It's a regular expression. It should cover a lot of cases. I use this one for e-mail validation. "$" indicates the end of the string, since an expression could match part of a string while there may be more characters after it.

Link to comment
Share on other sites

you can use "i" after the end delimiter of your regex to make it case insensitive

Link to comment
Share on other sites

Can anyone explain exactly how negating(^) a class works? For example, if this is the pattern: q[^x] and the string to be searched is 'question', what is returned(matched)? From my understanding, does the above mean 'match any character that is not an x'... or? Thanks.

Link to comment
Share on other sites

yes anchor inside character class means match everything except 'X'. though outside of character class anchor determine the position. as in OP email regex it tells it must have start like that pattern to match. no any character is not allowed before it.

Link to comment
Share on other sites

I usually don't use the case insensitive modifier (achieved by putting an "i" following the delimiter). If you want to use the expression in a case-sensitive context, just change all the A-Z for a-zA-Z The expression q[^x] will match "qu" in "question" but would fail to match "qx" in "qxestion"

Link to comment
Share on other sites

It's a regular expression. It should cover a lot of cases. I use this one for e-mail validation. "$" indicates the end of the string, since an expression could match part of a string while there may be more characters after it.
Thanks for the site. I think I will use this on: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$It seems to be the most accurate/useful one. Does the ^ apply to the whole expression or just the first []? I would assume the first []. On the site, it said:
First, long regexes make it difficult to nicely format paragraphs. So I didn't include a-z in any of the three character classes. This regex is intended to be used with your regex engine's "case insensitive" option turned on. (You'd be surprised how many "bug" reports I get about that.)
I may be misreading this, but does it say to use i as a modifier instead of adding a-z? If that is true, doesn't that affect the domain and tld meaning when I use test, it will always come back true since the domain and tld will always contain letters? When you use test function with the above expressions, how does it work? Does it only return one true/false or one for each of the []? For the domain and tld, wouldn't it be better to use ^ to prevent unwanted characters? Edited by dalawh
Link to comment
Share on other sites

  • 2 weeks later...
Does the ^ apply to the whole expression or just the first []? I would assume the first [].
Neither, it signifies the start of the string to test. The $ signifies the end, so the entire string must match the pattern rather than only part of the string matching.
I may be misreading this, but does it say to use i as a modifier instead of adding a-z?
Yes.
If that is true, doesn't that affect the domain and tld meaning when I use test
The modifiers apply to the entire pattern.
it will always come back true since the domain and tld will always contain letters?
If it is a valid domain and TLD then it will match. Domains don't need to contain only letters. All TLDs currently have only letters.
When you use test function with the above expressions, how does it work? Does it only return one true/false or one for each of the []?
The test method returns true if the string matches the pattern, or false otherwise. Since that pattern is matching against the entire string, it returns true if the entire string matches the pattern.
For the domain and tld, wouldn't it be better to use ^ to prevent unwanted characters?
In this case it's better to use a whitelist of allowed characters instead of trying to list every character that is not allowed.
Link to comment
Share on other sites

Neither, it signifies the start of the string to test. The $ signifies the end, so the entire string must match the pattern rather than only part of the string matching.
So when ^ is inside [], it means not and when ^ is outside of [], it means the start?
Yes. The modifiers apply to the entire pattern.
Without the i modifier, it will only accept the domain and tld in capitals right?
In this case it's better to use a whitelist of allowed characters instead of trying to list every character that is not allowed.
Oh. What I mean was, was it better to use [A-Z] and true means valid email or [^A-Z] and false means valid email. Pretty much the same, but different ways of looking at it. I realized that the way mentioned was better.
Link to comment
Share on other sites

The caret will only negate a character class if it is the first character. If it is not the first character then it is considered part of the character class. It if appears at the start of a pattern then it signifies the start of the test string. There's a reference here: http://www.regular-expressions.info/reference.html

Without the i modifier, it will only accept the domain and tld in capitals right?
Right.
What I mean was, was it better to use [A-Z] and true means valid email or [^A-Z] and false means valid email.
They're the same thing, neither is "better". The classes [A-Z] and [^A-Z] are mutually-exclusive.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...