开发者

Validating an e-mail field based on a specific domain

开发者 https://www.devze.com 2022-12-27 06:08 出处:网络
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. o

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开发者_StackOverflow中文版).

Any help would be appreciated!


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;
}


In this example my domain is "@uol.edu.pk". You can do it like this. 


  $(document).ready(function (e) {
                $('#SubmitButton').click(function () {
                    var email = $('#form-email').val();
                    // Checking Empty Fields
                    if ($.trim(email).length == 0 || $("#form-first-name").val() == "" || $("#form-password").val() == "" || $("#Password1").val()=="") {
                        alert('All fields are Required');
                        e.preventDefault();
                    }
                    if (validateEmail(email)) {
                        alert('Good!! your Email is valid');
                    }
                    else {
                        alert('Invalid Email Address');
                        e.preventDefault();
                    }
                });
            });
            // Function that validates email address through a regular expression.
            function validateEmail(pEmail) {
                var filterValue = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
                if (filterValue.test(pEmail)) {
                    if (pEmail.indexOf('@uol.edu.pk', pEmail.length - '@uol.edu.pk'.length) != -1)
                    {
                        return true;
                    }
                    else {
                        alert("Email Must be like(yourName@uol.edu.pk)");
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
    enter code here
0

精彩评论

暂无评论...
验证码 换一张
取 消