我有一个需要用户登录的网站。客户端将仅允许来自其公司域的用户获得访问权限。我需要根据电子邮件域地址验证该字段。 IE。仅允许来自 @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;
}
关于jquery - 根据特定域验证电子邮件字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2631169/