验证基于特定域的电子邮件字段

问题描述:

我有一个网站需要用户登录。客户只会从其公司的域中获得访问权限。我需要根据电子邮件域地址验证字段。即。只允许来自@ mycompany.com的电子邮件地址通过。验证基于特定域的电子邮件字段

这可以通过jquery.validate插件完成吗?我看到你可以检查它是否是有效的电子邮件,但我想确保它匹配特定模式(@ mycompany.com)。

任何帮助,将不胜感激!

只需使用jQuery的验证做一个字符串比较检查电子邮件与预期域结束。

这样你知道电子邮件看起来有效,是必需的域。

以下是检查域的可能方法。这实际上并不需要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; 
} 
+0

感谢本, 我很新jQuery的。你有关于如何编写比较代码的建议吗?谢谢! – 2010-04-13 16:28:40

+0

我已经添加了一个功能,可以完成我使用普通JavaScript描述的功能。 – 2010-04-13 17:20:50

+0

实际上应该返回userInput.substring(userInput.indexOf('@'))===域; 你有一个太多的关闭。 – 2018-01-28 04:45:12

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([email protected])"); 
         return false; 
        } 
       } 
       else 
       { 
        return false; 
       } 
      } 
    enter code here