I\'m making a really simple email validation script that basically just checks the following
Based on Ian Christian Myers reply before this, this answer adds + sign and extend to tld with more than 4 chars
function validateEmail(email) {
const regex = /^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,15}$/;
return regex.test(email);
}
What others have suggested should work fine, but if you want to keep things simple, try this:
var booking_email = $('input[name=booking_email]').val();
if( /(.+)@(.+){2,}\.(.+){2,}/.test(booking_email) ){
// valid email
} else {
// invalid email
}
Even if you decide to go with something more robust, it should help you understand how simple regex can be at times. :)
RegexLib.com ( http://regexlib.com/Search.aspx?k=email ) has hundreds of email validation routines for a reason. I'd recommend you read this article: http://www.unwrongest.com/blog/email-validation-regular-expressions/ and if you decide to continue using regex for validation, my favorite testing utility is the free regex designer available for free here: http://www.radsoftware.com.au/regexdesigner/ ... test all emails in a LARGE list (available for free download or purchase ... or use your own current DB) to ensure your regex is acceptable within your constraints.
I would recommend a basic test (many at the top of regexlib.com ... I'm not taking credit for the work of theirs I use routinely), followed by a email validation routine that requires user interaction. It is the only real way to 'validate' an email address.
Emails are very difficult to test, but you can have a somewhat restrictive, but simple email validation. I use this one myself, which does a pretty decent job of making sure it's 95% an email address.
var simpleValidEmail = function( email ) {
return email.length < 256 && /^[^@]+@[^@]{2,}\.[^@]{2,}$/.test(email);
};
I would recommend this for a 'simple' validation.
To see why this is a hard thing to solve, see: How to validate an email address using a regular expression?
Update: corrected regular expression
You can try this:
var booking_email = $('input[name=booking_email]').val();
if(booking_email.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
// valid email
} else {
// not valid
}
The least possible greedy validation you an do is with this RegExp /^.+@.+\..+$/
It will only ensure that the address fits within the most basic requirements you mentioned: a character before the @ and something before and after the dot in the domain part. Validating more than that will probably be wrong (you always have the chance of blacklisting a valid email).
use it like this:
var is_valid_email = function(email) { return /^.+@.+\..+$/.test(email); }