JSFiddle - React, Tailwind, and code Playground

by cillay

HTML

<form onsubmit="return false">
    <input type="text" name="email" size="60" maxlength="255" />
    <br /><br />
    <button>Validate Email</button>
</form>

JavaScript

$('button').eq(0).bind("click", function () {
    alert(validateEmailAddress(this.form.email.value));
});

// 12702.088
var validateEmailAddress = function(email) {
    var isValid = false;
    
    if (Boolean(email) && typeof(email) === "string") {
        // Straightforward simple test of complete email address
        isValid = /^[\w\.\-]+@[\w\.\-]+\.\w{2,9}$/.test(email);
        
        // The first character of email should be alphanumeric
        // Before @ it contains 1 or more characters
        // After first character it allows only 3 special characters (_ . -).
        isValid &= /^[A-Za-z0-9][A-Za-z0-9\-\._]*?@/.test(email);
        
        // Before @ it should ended up with alphanumeric.
        isValid &= /[A-Za-z0-9]@/.test(email);
        
        // It also not allows two consecutive special characters.
        isValid &= !/[\-\._]{2,}/.test(email);
        
        // Immediate char after @ is alphanumeric
        // After first character of domain name only allows 2 special characters (. -).
        isValid &= /@[A-Za-z0-9][A-Za-z0-9\-\.]*?$/.test(email);
    
        // The domain name should end with only alpha characters.
        // The extension length should be minimum of 2.
        isValid &= /\.[A-Za-z]{2,}$/.test(email);
    }
    
    return Boolean(isValid);
};