I got the javascript email validation code from this post. According to the all the answers the RFC2822 regex is as per following:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
I tried to include this in js, but it gives a syntax error.
var email = this.value;
if(email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/) )
{
//matched...
} else {
//not matched..
}
I am using eclipse, and it's giving the error as soon as I enter this code. Here are some of the errors from the long list
Multiple markers at this line
- Syntax error on tokens, ArrayLiteralHeader expected instead
- Syntax error on token "?", invalid Expression
- Syntax error on token "Invalid Character", delete this token
- Syntax error on tokens, delete these tokens
- Syntax error, insert ")" to complete Arguments
- Missing semicolon
- Syntax error on token "Invalid Character", delete this token
- Syntax error, insert ")" to complete PrimaryNoNewArray
- Syntax error on tokens, ArrayLiteralHeader expected instead
- Syntax error on token "^", invalid (
Please let me know how to properly include email validation regex into the code?
Tried in browser console. Exact same syntax works in chrome js console. Here is the output
Thanks
Javascript regex literal need /
to enclose it.
if(email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/) )
{
//matched...
} else {
//not matched..
}