简体   繁体   English

允许用户使用特定的电子邮件地址域进行注册

[英]Allow users to register with specific email address domain

I am trying to make a validation on a field, I want to allow users register only with specific domains' emails. 我想在一个字段上进行验证,我想允许用户仅使用特定域的电子邮件进行注册。 For eg I want users to register only the emails which are from the company1, company2, company3. 例如,我希望用户仅注册来自company1,company2,company3的电子邮件。 I use this code: 我使用以下代码:

HTML: HTML:

    <input type="text"/>
    <button>Run</button>

Jquery: jQuery:

$('button').on('click', function(){
str = $('input').val();
str = str.split('@').slice(1);

var allowedDomains = ['company1', 'company2', 'company3'];

if ($.inArray(str[0], allowedDomains) !== -1) {
    alert(str + ' is allowed');
}else{
    alert('not allowed');
}
});

and it working fine, but the problem i facing that if user enter domain name in uppercase or write first letter in uppercase then lowercase. 并且它可以正常工作,但是我面临的问题是,如果用户以大写形式输入域名或以大写形式写第一个字母,然后以小写形式书写。 How can i validate to accept both uppercase and lowercase. 我如何验证是否接受大写和小写。

You can compare the forbidden strings to the lowercase version of the user's input email . 您可以将禁止的字符串与用户输入电子邮件的小写版本进行比较。

if ($.inArray(str[0].toLowerCase(), allowedDomains) !== -1) {

Also, keep in mind that, if this is an important criteria to you, you should ensure that validations are also done server side, as it easy to bypass the client and send manually forged requests. 另外,请记住,如果这对您来说是重要的标准,则应确保也在服务器端进行验证,因为这很容易绕过客户端并发送手动伪造的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM