简体   繁体   中英

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. I use this code:

HTML:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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