JsEveryDay
JsEveryDay

Reputation: 323

Validate/accept only emails from a specific domain name

This is part of my jQuery script. I need to make the system validate emails for a specific domain.

like [email protected]

And only allow emails from @schooldomain.com

Code:

email: function(value,element){return this.optional(element)||/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);}

Upvotes: 5

Views: 23167

Answers (2)

Henrik Granum
Henrik Granum

Reputation: 1

Thanks to this thread I found another solution for only accepting one specific domain after the "at" / "@". Get everything after the dash in a string in JavaScript Basically dividing the email in two, the text before @ and the text after @. If the text after @ is not equal to the specified domain the validation will be false.

 // Email validation   
let em = document.forms['Login']['email'].value; 
let atpos = em.indexOf("@");
let domain = em.split("@")[1]; // Saves user input after the @ (at)

if (em == null || em == "") {
    alert("Email can not be empty.");
    document.getElementById('e').focus();
    return false;
} 
                                            // First test checks for atleast one character before @
else if (atpos < 1 || domain != "gmail.com"){ // Second test checks if the user entered a gmail.com domain after @
    alert("Not a valid e-mail address. Please write your gmail address like this: [email protected].");
    document.getElementById('e').focus();
    return false;
} 

Upvotes: 0

Jacob Lauritzen
Jacob Lauritzen

Reputation: 2840

Firstly, as pointed out in the comments, validate the email using regex, and then check if the email is from the right domain.

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if(re.test(email)){
        //Email valid. Procees to test if it's from the right domain (Second argument is to check that the string ENDS with this domain, and that it doesn't just contain it)
        if(email.indexOf("@thedomain.com", email.length - "@thedomain.com".length) !== -1){
            //VALID
            console.log("VALID");
        }
    }
}

Upvotes: 12

Related Questions