如何验证电子邮件地址是唯一的 [英] How to verify that email addresses are unique
本文介绍了如何验证电子邮件地址是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个JavaScript或jQuery解决方案。
解决方案
要在数组中找到重复项,请将元素插入到字典中,以跟踪每个元素出现的次数。那么计数大于1的每个元素都是重复的。
演示:
function findDuplicates(data){var counts = {}; data.forEach(function(item){if(counting [item] === undefined){counting [item] = 1;} else {counting [item] + = 1; if(counting [item] == 2){ print('duplicate:'+ item);}}});} function print(s){document.write(s +'< br />');} var addresses = ['abc@xyz.com' ,'foo@bar.com','abc@xyz.com','babar@elephants.com','president@whitehouse.gov','babar@elephants.com','abc@xyz.com']; findDuplicates(addresses);
I have a form containing 10 to 15 email addresses. How can I check whether all addresses are unique? And how can I determine which ones are duplicates?
I'm looking for a JavaScript or jQuery solution.
解决方案
To find duplicates in an array, insert the elements into a dictionary that keeps track of the number of times each element appears. Then each element with a count greater than 1 is a duplicate.
Demonstration:
function findDuplicates(data) {
var counts = {};
data.forEach(function (item) {
if (counts[item] === undefined) {
counts[item] = 1;
} else {
counts[item] += 1;
if (counts[item] == 2) {
print('duplicate: ' + item);
}
}
});
}
function print(s) {
document.write(s + '<br />');
}
var addresses = ['abc@xyz.com', 'foo@bar.com', 'abc@xyz.com', 'babar@elephants.com', 'president@whitehouse.gov', 'babar@elephants.com', 'abc@xyz.com'];
findDuplicates(addresses);
这篇关于如何验证电子邮件地址是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文