在JavaScript中验证设置为JSON的电子邮件扩展名列表

时间:2016-10-12 22:29:20

标签: javascript json regex

如何验证用户输入的电子邮件并使用JSON中的电子邮件扩展列表检查其扩展名?

就像我输入abc@efg.com一样,它只会在JSON列表中检查@ efg.com的电子邮件扩展名。

或者一个正则表达式,它只能获得" @"之后的值。在此之前忽略任何事情。

[
    {
        "School": "Ivy Tech Community College",
        "Email": "ivytech.edu"
    },
    {
        "School": "Liberty University",
        "Email": "liberty.edu"
    },
    {
        "School": "Miami Dade College",
        "Email": "mdc.edu"
    },
    {
        "School": "Lone Star College",
        "Email": "lonestar.edu"
    },
    {
        "School": "Ashford University",
        "Email": "ashford.edu"
    }
]

4 个答案:

答案 0 :(得分:2)

// initial data
var data = '[ {"School":"Ivy Tech Community College","Email":"ivytech.edu"},' + '{"School":"Liberty University","Email":"liberty.edu"},' + '{"School":"Miami Dade College","Email":"mdc.edu"},' + '{"School":"Lone Star College","Email":"lonestar.edu"},' + '{"School":"Ashford University","Email":"ashford.edu"} ]';

// json-ify our data
var jsonData = JSON.parse(data);

// map the values of each JSON 'Email' property from jsonData in an array 
var emailsArray = jsonData.map(function (x) { return x.Email; });

// email address for testing
var testEmail = "john@liberty.edu";

// split the email address by the "@" character and use the second part (domain)
if (arrayContains(testEmail.split("@")[1], emailsArray))
{
    // this will fire as john@liberty.edu matches liberty.edu in emailsArray
    console.log("emailsArray contains domain");
}
else
{
    console.log("emailsArray does not contain domain");
}

// function to check if an item is contained in an array
function arrayContains(item, array)
{
    return (array.indexOf(item) > -1);
}

完成JSFiddle示例here

注意:

  • 您可以忽略前两行代码,因为我猜你从Web响应中获取了JSON数据
  • 正在假设testEmail遵守电子邮件地址的格式;您可能需要实现某种验证来验证输入的字符串是否是实际的电子邮件
  • 我们将testEmail分割为@字符并使用String.prototype.split()
  • 使用Array.prototype.map()函数
  • 创建emailsArray数组
  • arrayContains使用String.prototype.indexOf()方法检查testEmail
  • 中是否存在emailsArray

我想我已经澄清了示例中每行代码的作用。您现在可以根据自己的要求进行调整,甚至更好地改进它。

答案 1 :(得分:0)

而不是正则表达式,您可以遍历学校数组,并像这样匹配域:

private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ...

    pnlWagens1 = new pnlWagens();
    UpdateGUI();
}                                        

private void UpdateGUI(){
    pnlWagens1.repaint();
}

您可以将var schools = [ {"School":"Ivy Tech Community College","Email":"ivytech.edu"}, {"School":"Liberty University","Email":"liberty.edu"}, {"School":"Miami Dade College","Email":"mdc.edu"}, {"School":"Lone Star College","Email":"lonestar.edu"}, {"School":"Ashford University","Email":"ashford.edu"} ] function validate(email) { var domain = email.split('@').pop(); for(var i = 0; i < schools.length; i++) { if(domain === schools[i].Email) return schools[i].School; } return 'Domain Not Found'; } 替换为schools[i].School,将true替换为'Domain Not Found',只需检查它是否存在。

示例:https://jsfiddle.net/TheQueue841/gwhkq520/

答案 2 :(得分:0)

像提到的trashrOx之类的东西会起作用:

var edus = [
    {"School":"Ivy Tech Community College","Email":"ivytech.edu"},
    {"School":"Liberty University","Email":"liberty.edu"},
    {"School":"Miami Dade College","Email":"mdc.edu"},
    {"School":"Lone Star College","Email":"lonestar.edu"},
    {"School":"Ashford University","Email":"ashford.edu"}
];

var emails = ['matty.fake@lonestar.edu','himom@mdc.edu','randomguy@yahoo.com'];

emails.forEach(function(element,index) {
  var domain = element.substring(element.indexOf('@') + 1);
  var match = 'none';
  if (domain) {
    edus.forEach(function(element,ind) {
      if (element.Email === domain) {
        match = element.School;
      }
    });
    console.log(element + ' matched ' + match);
  }
});

// matty.fake@lonestar.edu matched Lone Star College
// himom@mdc.edu matched Miami Dade College
// randomguy@yahoo.com matched none

答案 3 :(得分:0)

如果您只想验证,请使用Array.prototype.some()

some()对数组中存在的每个元素执行一次回调函数,直到找到一个回调返回truthy值的值(转换为布尔值时变为true的值)。如果找到这样的元素,some()会立即返回true。否则,some()返回false。仅为已分配值的数组的索引调用回调;对于已删除或从未分配过值的索引,不会调用它。

var schools = [
    {"School":"Ivy Tech Community College","Email":"ivytech.edu"},
    {"School":"Liberty University","Email":"liberty.edu"},
    {"School":"Miami Dade College","Email":"mdc.edu"},
    {"School":"Lone Star College","Email":"lonestar.edu"},
    {"School":"Ashford University","Email":"ashford.edu"}
]

function validate(email) {
    var domain = email.split('@').pop();
    return schools.some(function(school) {
    	return school.Email === domain;
    });
}

validate('test@ashford.edu');