5

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

like example@schooldomain.com

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);}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
JsEveryDay
  • 313
  • 2
  • 5
  • 16

2 Answers2

12

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");
        }
    }
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
Jacob Lauritzen
  • 2,690
  • 1
  • 18
  • 17
  • 2
    This code will say `@thedomain.comTHIS_IS_CERTAINLY_NOT_AN_EMAIL_ADDRESS@@` is valid. – kapa Aug 29 '12 at 11:03
  • Edited so it firsts validates the email, then checks if it's from the right domain. – Jacob Lauritzen Aug 29 '12 at 15:34
  • 2
    IIRC email isn't regexable, there will always be some hypothetical address that will not match the regex, but will be valid. I think the best solution is just to check a) is there only one `@` in the address, b) does the string match `"@thedomain.com$"` Edit: turns out, it is regexable, but with one **BEAST** of a regex: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html – DZittersteyn Aug 29 '12 at 15:43
  • Well, I thought this was the best method (that was simple). – Jacob Lauritzen Aug 29 '12 at 18:32
  • @JacobLauritzen `indexOf` is still not the right method, because you can't check if it's at the end or not. `name@thedomain.com.something.else.com` is a valid email-address... – kapa Aug 29 '12 at 21:07
  • 2
    Fixed that, by checking if it ends with it. – Jacob Lauritzen Aug 30 '12 at 12:56
0

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: username@gmail.com.");
    document.getElementById('e').focus();
    return false;
}