Skip to main content
One regex used anchors while the other did not, without explanation. Edited for consistency and explanation
Source Link
Regular Jo
  • 5.4k
  • 3
  • 25
  • 49

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\^\S+@\S+\.\S+\S+$/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

The above regexes match the whole string, remove the leading and ^ and trailing $ if you want to match anywhere in the string. The example below matches anywhere in the string.

If you do want to match the whole sring, you may want to trim() the string first.

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
}
    }
console.log(validateEmail('my email is [email protected]')); // true
    
console.log(validateEmail('anystring@anystring'my email is anystring@anystring .anystring'any')); // false

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('anystring@anystring.anystring'));

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/^\S+@\S+\.\S+$/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

The above regexes match the whole string, remove the leading and ^ and trailing $ if you want to match anywhere in the string. The example below matches anywhere in the string.

If you do want to match the whole sring, you may want to trim() the string first.

Example JavaScript function:

function validateEmail(email) {
  var re = /\S+@\S+\.\S+/;
  return re.test(email);
}
    
console.log(validateEmail('my email is [email protected]')); // true
    
console.log(validateEmail('my email is anystring@anystring .any')); // false

fix regex
Source Link
Akaisteph7
  • 6k
  • 2
  • 27
  • 48

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+$^[^\s@]+@[^\s@]+\.[^\s@]+$/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('[email protected]'));

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+$/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('[email protected]'));

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('[email protected]'));

added 74 characters in body
Source Link
Nick Bull
  • 9.7k
  • 6
  • 38
  • 63

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+$/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('[email protected]'));

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('[email protected]'));

I've slightly modified Jaymon's answer for people who want really simple validation in the form of:

[email protected]

The regular expression:

/\S+@\S+\.\S+/

To prevent matching multiple @ signs:

/^[^\s@]+@[^\s@]+$/

Example JavaScript function:

function validateEmail(email) 
    {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
    
console.log(validateEmail('[email protected]'));

just made the code executable and add a link
Source Link
the-breaker
  • 195
  • 1
  • 14
Loading
edited for readability
Source Link
the Tin Man
  • 160k
  • 44
  • 218
  • 306
Loading
Rollback to Revision 6
Source Link
C. Lee
  • 3.3k
  • 2
  • 23
  • 21
Loading
added 10 characters in body
Source Link
tk_
  • 17k
  • 9
  • 83
  • 90
Loading
Rollback to Revision 4 - Roll back to revision 2
Source Link
C. Lee
  • 3.3k
  • 2
  • 23
  • 21
Loading
Modified regex in accordance with what the author agreed to, plus clarifications.
Source Link
André Chalella
  • 14.1k
  • 10
  • 56
  • 64
Loading
Rollback to Revision 2
Source Link
Flexo
  • 88.3k
  • 22
  • 194
  • 278
Loading
udpated regexp
Source Link
Robin C Samuel
  • 1.2k
  • 15
  • 33
Loading
Post Made Community Wiki by pera
Copy edited. Removed meta information (this belongs in comments, if any).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
Source Link
C. Lee
  • 3.3k
  • 2
  • 23
  • 21
Loading