4

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?

EDIT

Tried in browser console. Exact same syntax works in chrome js console. Here is the output

Web console

Thanks

1
  • Apparently / don't close the literal when it is inside a character class, but Eclipse might not understand this. Escape all / inside the expression. Commented Sep 2, 2012 at 12:39

4 Answers 4

3

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..
}
Sign up to request clarification or add additional context in comments.

4 Comments

Please, check the same thing in eclipse if you can. I can't get what's wrong? Same error as posted in the questions.
@mtk Just try it in the browser console.
@xdazz: yeah, I just noticed that, sorry. But this could still be the reason for the error in Eclipse.
@FelixKling yes, that may be the reason ~
1

In Javascript, you use regexes like the below:

myString.match(/<regexHere>/)

1 Comment

I have no experience with eclipse. I am guessing it has some bug with (basic) Javascript grammar. Could you try executing it directly?
0

the regex must be encapsulated by "/" in javascript:

    if (/[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.test(subject)) {
        // Successful match
    } else {
        // Match attempt failed
    }

3 Comments

Please, check the same thing in eclipse if you can. I can't get what's wrong? Same error as posted in the questions.
eclipse? are you sure that you're doing JavaSCRIPT?
yes, I am doing a php project with some css, js, and obviously html.
0

You can clean you E-Mail-Address like this:

// Function to get a cleaned email address
function getEmailCleaned(email) {
    // Regex for allowed characters in an email address
    const regex = /[a-z0-9!#$%&'*+/=?^_`{|}~.@-]/gi;

    // Filter only the allowed characters from the input
    const cleanedEmail = email.match(regex)?.join('') || '';

    // Verify that the cleaned email address is valid
    const emailRegex = /^[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;
    if (emailRegex.test(cleanedEmail)) {
        return cleanedEmail.trim(); // Removes unnecessary spaces
    } else {
        return null; // The cleaned email address is invalid
    }
}

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.