验证消息被添加两次

时间:2018-01-08 07:47:37

标签: javascript jquery

我正在从jQuery动态添加错误消息div,但即使我检查条件是否正常工作,错误消息也会被添加两次。

在我的代码下面:



$('.refEmail').on('focusout', function() {
  var reg = /^(([^<>()\[\]\\.,;:\s@@"]+(\.[^<>()\[\]\\.,;:\s@@"]+)*)|(".+"))@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

  if ($(this).val() != "" && !$(this).val().match(reg)) {

    if ($(this).next(".validation").length == 0) // only add if not added
    {
      $(this).after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please enter valid email</div>");
    }
  } else {
    $(this).next(".validation").remove();
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="refEmail" id="ReferralEmail" name="ReferralEmail" type="text" value="">
&#13;
&#13;
&#13;

error

0 个答案:

没有答案
相关问题