如何清除文件中的无效电子邮件地址

发布于 2024-11-01 04:48:37 字数 312 浏览 3 评论 0原文

我正在寻找一个脚本或可以获取 csv 文件作为输入的东西。 它将逐行解析文件并检查当前行是否包含有效的电子邮件(例如: [电子邮件受保护]

我认为这一定已经存在于某个地方。

带有一些 javascript/jquery 的本地 html 文件将是完美的。

我需要这个来检查手动输入电子邮件且无需验证的列表。

谢谢 米歇尔

I'm looking for a script or something that would get a csv file as imput.
It would parse the file line by line and check if the current line contains a valid email (e.g. : [email protected])

I think this must already exist somewhere.

A local html file with some javascript/jquery would be perfect.

I need this to check lists with manually entered emails with no verification.

Thanks
Michel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧情别恋 2024-11-08 04:48:37

你将无法使用 javascript 读取或写入本地文件,我是用 ruby​​ 编写的。如果代码对您没有用,也许正则表达式会对您有用。

#!/usr/bin/ruby
File.open("somefile.csv").each{ |line|
    if line =~ /\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}/
        puts "Good email!"
    else
        puts "FAIL"
    end
}

You wont be able to read or write local files with javascript, I wrote this in ruby. If the code isn't useful to you maybe the regular expression will.

#!/usr/bin/ruby
File.open("somefile.csv").each{ |line|
    if line =~ /\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}/
        puts "Good email!"
    else
        puts "FAIL"
    end
}
小巷里的女流氓 2024-11-08 04:48:37

1) 请注意,验证电子邮件地址非常困难。事实上,想要做到完美是不可能的。这是表达复杂性、覆盖范围和准确性(误报)之间的权衡。请参阅 SO 上有关验证电子邮件地址的无数其他问题,也请参阅此处: http:// /www.regular-expressions.info/email.html

2) 您有一个平面文件 (.csv)。您无法使用 javascript 读取此内容并在浏览器中进行处理。您需要查看其他语言。 Perl 和 Java 随机提及两种语言都有良好的正则表达式支持。

1) Be warned that validating email addresses is extremely hard. In fact it's impossible to do perfectly. It's a trade-off between expression complexity, coverage, and accuracy (false positives). See the zillions of other questions on SO about validating e-mail addresses, and also see here: http://www.regular-expressions.info/email.html

2) You have a flat file (.csv). You can't read this in with javascript and process in a browser. You'll need to look at some other language. Perl and Java to randomly mention two languages have good regex support.

东走西顾 2024-11-08 04:48:37

由于您正在寻找 Javascript 解决方案,这里有一些简单的 JS,假设您有一些包含 csv 数据的文件,如下所示:

<pre id="csv">
a,b,[email protected]!tld
1,2,[email protected]
4,5,[email protected]
</pre>

这是您可能想要替换分隔符、使用的换行符或用于检查地址的正则表达式的脚本。根据您的验证要求,请在此处查找其他正则表达式:在 JavaScript 中验证电子邮件地址?

var separator = ',',
    linebreak = '\n',
    regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
    csv = document.getElementById('csv'),
    lines = csv.innerHTML.split(linebreak),
    fields,
    i;

for(i = 0; i < lines.length; i++)
{
    fields = lines[i].split(separator);
    if (regex.test(fields[mailColumn]))
    {
        document.write(fields[mailColumn] + ' is valid<br/>');
    }
}

Since you looked for a Javascript solution, here is some plain JS assuming you have some file containing the csv data like this:

<pre id="csv">
a,b,[email protected]!tld
1,2,[email protected]
4,5,[email protected]
</pre>

Here is the script where you might want to replace the seperator, the linebreaks used or the regular expression for checking the addresses. Depending on your validation requirements have look for other regular expressions here: Validate email address in JavaScript?

var separator = ',',
    linebreak = '\n',
    regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
    csv = document.getElementById('csv'),
    lines = csv.innerHTML.split(linebreak),
    fields,
    i;

for(i = 0; i < lines.length; i++)
{
    fields = lines[i].split(separator);
    if (regex.test(fields[mailColumn]))
    {
        document.write(fields[mailColumn] + ' is valid<br/>');
    }
}
篱下浅笙歌 2024-11-08 04:48:37

您好,感谢您的回答。
我最终设法使用本地 html 文件和 javascript 来完成它。
方法如下:

  1. 有一个本地网络服务器(例如 xampp)
  2. 创建一个 html 文件,该文件将使用 jquery(例如)在 AJAX 中加载 csv 文件
  3. 将加载的文件以 \n(换行符)字符拆分为数组
  4. 处理每个数组根据需要的元素(再次拆分为“;”等)
  5. 填写两个文本区域字段:一个包含有效的电子邮件地址,另一个包含无效的电子邮件地址
  6. 手动修复或删除无效文本区域中的地址
  7. 将有效文本区域复制/粘贴到新的干净文本区域 :

为了使其正常工作,我将 csv 文件放在 html 文件旁边,然后使用“输入类型=文件”来加载它

,瞧:-)

原始代码

<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validation email</title>
<script src="jquery.js"></script>
<script>
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};


function no_accent (my_string) {
    var new_string = "";
    var pattern_accent         = new Array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ý','þ','ÿ');
    var pattern_replace_accent = new Array('A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I','D','N','O','O','O','0','O','O','U','U','U','U','Y','b','s','a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','u','u','u','u','y','y','b','y');
    if (my_string && my_string!= "") {
        new_string = preg_replace (pattern_accent, pattern_replace_accent, my_string);
    }
    return new_string;
}

$(document).ready(function() {
    $('#checkMail').click(function() {

    $('#invalid').val('');
      $('#valid').val('');

      $.ajax({
        type: "GET",
        url: $('#fileName').val(),
        dataType: "text",
        cache:false,
        success: function(text) {
          alert("Start process");
          var reg=new RegExp("\r\n", "g");
          var monTab = text.split(reg);

          for (cpt=0;cpt<monTab.length;cpt++){


//do some custom check here if needed

                if (isValidEmailAddress(monTab[cpt])){
                    //add to valid textarea
                    document.getElementById('valid').value += monTab[cpt] + "\r\n";
                } else {
                    //add to invalid textarea
                    document.getElementById('invalid').value += monTab[cpt] + "\r\n";
                }
            }
          alert("Process over!");
        }
      });//close $.ajax 
    });
});

</script>
</head>
<body>
<input type="file" name="myfile" size="50" id="fileName"> (put csv file next to this html file)<br/>
<input type="button" value="Process" id="checkMail">
<br/>
Invalid adresses : <br/>
<textarea id="invalid" cols="80" rows="20"></textarea>
<br/>
Valid adresses : <br/>
<textarea id="valid" cols="80" rows="20"></textarea>
</body>
</html>

Hello and thank you for your answers.
I eventually managed to do it with a local html file and javascript.
Here is the method:

  1. Have a local webserver (like xampp)
  2. Create an html file that will use jquery (for example) to load the csv file in AJAX
  3. Split the loaded file on the \n (newline) character to an array
  4. Process each array element according to what is needed (split again on ";" etc.)
  5. Fill two textarea fields : one with valid email adress, the other with invalid ones
  6. Manually fix or delete adresses from invalid textarea
  7. Copy/Past the valid textarea to a new clean file

In order to have it working, I place the csv file next to the html file, then I use a "input type=file" to load it

And voila :-)

Raw code :

<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validation email</title>
<script src="jquery.js"></script>
<script>
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};


function no_accent (my_string) {
    var new_string = "";
    var pattern_accent         = new Array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ý','þ','ÿ');
    var pattern_replace_accent = new Array('A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I','D','N','O','O','O','0','O','O','U','U','U','U','Y','b','s','a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','u','u','u','u','y','y','b','y');
    if (my_string && my_string!= "") {
        new_string = preg_replace (pattern_accent, pattern_replace_accent, my_string);
    }
    return new_string;
}

$(document).ready(function() {
    $('#checkMail').click(function() {

    $('#invalid').val('');
      $('#valid').val('');

      $.ajax({
        type: "GET",
        url: $('#fileName').val(),
        dataType: "text",
        cache:false,
        success: function(text) {
          alert("Start process");
          var reg=new RegExp("\r\n", "g");
          var monTab = text.split(reg);

          for (cpt=0;cpt<monTab.length;cpt++){


//do some custom check here if needed

                if (isValidEmailAddress(monTab[cpt])){
                    //add to valid textarea
                    document.getElementById('valid').value += monTab[cpt] + "\r\n";
                } else {
                    //add to invalid textarea
                    document.getElementById('invalid').value += monTab[cpt] + "\r\n";
                }
            }
          alert("Process over!");
        }
      });//close $.ajax 
    });
});

</script>
</head>
<body>
<input type="file" name="myfile" size="50" id="fileName"> (put csv file next to this html file)<br/>
<input type="button" value="Process" id="checkMail">
<br/>
Invalid adresses : <br/>
<textarea id="invalid" cols="80" rows="20"></textarea>
<br/>
Valid adresses : <br/>
<textarea id="valid" cols="80" rows="20"></textarea>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文