tags:

views:

38

answers:

1

I have a site that will require a login by the users. The client will only only users from their company's domain to gain access. I need to validate the field based on an e-mail domain address. ie. only allow email addresses from @mycompany.com to go through.

Can this be done with the jquery.validate plugin? I see where you can check to see if it's a valid e-mail, but I'd like to make sure it matches a specific pattern (@mycompany.com).

Any help would be appreciated!

+3  A: 

Simply use the jQuery validate, and do a string comparison to check that the email ends with the expected domain.

That way you know the email appears valid, and is of the required domain.

Here's a possible method of checking the domain. This doesn't actually require jQuery.

/**
 * Checks that the user's email is of the correct domain.
 * 
 * userInput: potential email address
 * domain:    Correct domain with @, i.e.: "@mycompany.com"
 * Returns:   true iff userInput ends with the given domain.
 */
function checkDomain(userInput, domain) {
        // Check the substring starting at the @ against the domain
        return (userInput.substring(userInput.indexOf('@')) === domain;
}
Ben S
Thanks Ben,I'm very new to jQuery. Do you have any recommendations on how to write that comparison code? Thanks!
mmsa
I've added a function that does what I describe using plain javascript.
Ben S