JS-将单个电子邮件验证模式转换为多个电子邮件验证模式。

时间:2018-08-16 12:39:49

标签: javascript regex

我正在努力创建一个Regex模式来验证多封电子邮件。我知道这个话题已经被广泛讨论,但是,在对它们进行研究之后,我找不到我的具体问题的答案。我的问题如下。

我正在处理的项目是用PHP编写的,并且使用FILTER_VALIDATE_EMAIL。我的目的是编写一种前端电子邮件验证程序,以与FILTER_VALIDATE_EMAIL一致的方式用于单个和多个电子邮件。在https://github.com/mpyw/FILTER_VALIDATE_EMAIL.js处,我找到了符合最新RFC标准规定的理想Regex模式,即:

^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126})+(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$

现在,我一直在尝试为多封电子邮件使用这种特殊的模式功能。我的问题来了,我该怎么办?我正在尝试设置

[\s*,]*

为了使多个地址都能通过,到目前为止,我头疼不已,因此,非常感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

尝试一下。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" action="">
   <hr><br><br>
   <button type="button" id="valider">Valider</button>
   <button type="button" name="bouton" value="Add Row" class="add-row">Add row</button>
   <button type="button" class="delete-row">Delete Row</button><br><br><hr><br><br>
   <table>
    <thead>
      <tr>
        <th>Select</th>
        <th>Quantité</th>
        <th>Désignation</th>
        <th>Prix Unitaire</th>
        <th>Prix taxé</th>
      </tr>
    </thead>
    <tbody>
     <tr>
      <td><input type="checkbox" name="record" ></td>
      <td><input type="text" name="quantite" placeholder="quantité" class="quantite"></td>
     <td><select type='text' class='designation' name="designation">
            <option>Stylo</option>
            <option>Cahier</option>
            <option>Souris</option>
            <option>Clavier</option>
          </select>
       <td><input type="text" name="prix-unitaire" placeholder="prix unitaire" class="prix-unitaire"></td>
       <td><span class="pt"></span></td>
     </tr>
   </tbody>
 </table>
</form>

用于多封电子邮件。

var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i

if(!pattern.test(inputYour))
{
  console.log('not a valid e-mail address');
}​

验证功能应具有您的上述代码...

这是一次性检查...

var x = getEmails();
var emails = x.split(",");
emails.forEach(function (email) {
 validate(email.trim());
});

链接https://www.experts-exchange.com/questions/28940315/JavaScript-REGEX-validate-multiple-emails-addresses-separated-my-commas.html