在模糊表单验证中使用 JavaScript 时遇到问题

having trouble with javascript on blur form validation

本文关键字:JavaScript 遇到 问题 模糊 表单 验证      更新时间:2023-09-26

我不是JS最好的,但我正在尝试让表单即时验证客户端。我的问题是验证需要有效电子邮件且是强制性的电子邮件字段。强制性部分效果很好,但电子邮件验证部分不起作用。有人可以帮忙吗?下面是问题的笔和代码。

var defaultForm = (function() {
  var init = function() {
    // Initial hides
    $('.error').hide();
  };
  return {
    init: init
  };
}());
(function() {
  var
    error = $('.error'),
    isNotEmpty = false,
    numberRegEx = /[0-9]|'./,
    emailRegEx = /^(([^<>()'[']''.,;:'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,}))$/,
    $this;
  defaultForm.init();
  // Check for empty mandatory fields
  $('input').each(function() {
    $(this).on('blur', function() {
      $this = $(this);
      if ($this.val() !== "" && $this.val() !== "---") {
        isNotEmpty = true;
      } else if (typeof($this.attr('required')) === typeof undefined) {
        isNotEmpty = true;
      } else {
        isNotEmpty = false;
      }
      if (isNotEmpty === true) {
        $this.closest('.has-feedback').addClass('has-success');
        $this.closest('.has-feedback').removeClass('has-error');
        $this.next('span').hide();
        $this.parent().next('.error').hide();
      } else {
        $this.closest('.has-feedback').addClass('has-error');
        $this.closest('.has-feedback').removeClass('has-success');
        $this.next('span').show();
        $this.parent().next('.error').show();
      }
    });
  });
  // Check for non-numeric input in number fields
  $('input[type=number]').on('blur', function() {
    $this = $(this);
    if (numberRegEx.test($(this).val()) === true) {
      $this.closest('.has-feedback').addClass('has-success');
      $this.closest('.has-feedback').removeClass('has-error');
      $this.next('span').hide();
    } else {
      $this.closest('.has-feedback').addClass('has-error');
      $this.closest('.has-feedback').removeClass('has-success');
      $this.next('span').show();
      $this.next('span').html('<svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Please enter a valid number');
    }
  });
  // Check for non-numeric input in number fields
  $('input[type=emai]').on('blur', function() {
    $this = $(this);
    if (emailRegEx.test($(this).val()) === true) {
      $this.closest('.has-feedback').addClass('has-success');
      $this.closest('.has-feedback').removeClass('has-error');
      $this.next('span').hide();
    } else {
      $this.closest('.has-feedback').addClass('has-error');
      $this.closest('.has-feedback').removeClass('has-success');
      $this.next('span').show();
      $this.next('span').html('<svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Please enter a valid email address');
    }
  });
}());
.panel-grey {
  background-color: #f8f8f8;
  border: 1px solid #d6d7d7;
  padding: 1.25rem;
  margin-top: 1rem;
}
.icon {
  width: 16px;
  height: 16px;
}
.has-error .form-control {
  border-color: #cc0000;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}
.has-error span.error {
  color: #cc0000;
}
.has-error span.error .icon {
  fill: #cc0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div style="display:none">
  <svg width="0" height="0" style="position:absolute">
    <symbol viewBox="0 0 20 20" id="warning">
      <path d="M19.512 17.982l-8.908-15.63a.696.696 0 0 0-1.208 0L.49 17.98c-.122.212-.12.47.004.68s.352.34.598.34h17.815c.244 0 .473-.13.598-.34s.126-.468.007-.68zm-8.412-.98H8.9v-2h2.2v2zm0-3.5H8.9v-6h2.2v6z"></path>
    </symbol>
  </svg>
</div>
<form>
  <div class="panel-app-personal-name panel-grey">
    <div class="form-group">
      <label for="control_COLUMN4" class="col-sm-4">Name*</label>
      <div class="col-sm-6 has-feedback">
        <input type="" name="" class="form-control form-control-md" id="control_COLUMN4" placeholder="Full name" aria-describedby="nameTextFieldError" required="">
        <span id="nameTextFieldError" class="error" style="display: none;"><svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Please enter your name</span>
      </div>
      <div class="clearfix"></div>
    </div>
    <div class="form-group">
      <label for="control_EMAIL" class="col-sm-4">Email address*</label>
      <div class="col-sm-6 has-feedback">
        <input type="text" name="" class="form-control form-control-md" id="control_EMAIL" placeholder="Email address" aria-describedby="emailError" value="" required="">
        <span id="emailError" class="error" style="display: none;"><svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Email address is mandatory</span>
      </div>
      <div class="clearfix"></div>
    </div>
</form>

2 个小问题。

错过了$('input[type=emai]')l

您的电子邮件type="text":-

<input type="text" name="" class="form-control form-control-md" id="control_EMAIL" placeholder="Email address" aria-describedby="emailError" value="" required="">

var defaultForm = (function() {
  var init = function() {
    // Initial hides
    $('.error').hide();
  };
  return {
    init: init
  };
}());
(function() {
  var
    error = $('.error'),
    isNotEmpty = false,
    numberRegEx = /[0-9]|'./,
    emailRegEx = /^(([^<>()'[']''.,;:'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,}))$/,
    $this;
  defaultForm.init();
  // Check for empty mandatory fields
  $('input').each(function() {
    $(this).on('blur', function() {
      $this = $(this);
      if ($this.val() !== "" && $this.val() !== "---") {
        isNotEmpty = true;
      } else if (typeof($this.attr('required')) === typeof undefined) {
        isNotEmpty = true;
      } else {
        isNotEmpty = false;
      }
      if (isNotEmpty === true) {
        $this.closest('.has-feedback').addClass('has-success');
        $this.closest('.has-feedback').removeClass('has-error');
        $this.next('span').hide();
        $this.parent().next('.error').hide();
      } else {
        $this.closest('.has-feedback').addClass('has-error');
        $this.closest('.has-feedback').removeClass('has-success');
        $this.next('span').show();
        $this.parent().next('.error').show();
      }
    });
  });
  // Check for non-numeric input in number fields
  $('input[type=number]').on('blur', function() {
    $this = $(this);
    if (numberRegEx.test($(this).val()) === true) {
      $this.closest('.has-feedback').addClass('has-success');
      $this.closest('.has-feedback').removeClass('has-error');
      $this.next('span').hide();
    } else {
      $this.closest('.has-feedback').addClass('has-error');
      $this.closest('.has-feedback').removeClass('has-success');
      $this.next('span').show();
      $this.next('span').html('<svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Please enter a valid number');
    }
  });
  // Check for non-numeric input in number fields
  $('input[type=email]').on('blur', function() {
    $this = $(this);
    if (emailRegEx.test($(this).val()) === true) {
      $this.closest('.has-feedback').addClass('has-success');
      $this.closest('.has-feedback').removeClass('has-error');
      $this.next('span').hide();
    } else {
      $this.closest('.has-feedback').addClass('has-error');
      $this.closest('.has-feedback').removeClass('has-success');
      $this.next('span').show();
      $this.next('span').html('<svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Please enter a valid email address');
    }
  });
}());
.panel-grey {
  background-color: #f8f8f8;
  border: 1px solid #d6d7d7;
  padding: 1.25rem;
  margin-top: 1rem;
}
.icon {
  width: 16px;
  height: 16px;
}
.has-error .form-control {
  border-color: #cc0000;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}
.has-error span.error {
  color: #cc0000;
}
.has-error span.error .icon {
  fill: #cc0000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div style="display:none">
  <svg width="0" height="0" style="position:absolute">
    <symbol viewBox="0 0 20 20" id="warning">
      <path d="M19.512 17.982l-8.908-15.63a.696.696 0 0 0-1.208 0L.49 17.98c-.122.212-.12.47.004.68s.352.34.598.34h17.815c.244 0 .473-.13.598-.34s.126-.468.007-.68zm-8.412-.98H8.9v-2h2.2v2zm0-3.5H8.9v-6h2.2v6z"></path>
    </symbol>
  </svg>
</div>
<form>
  <div class="panel-app-personal-name panel-grey">
    <div class="form-group">
      <label for="control_COLUMN4" class="col-sm-4">Name*</label>
      <div class="col-sm-6 has-feedback">
        <input type="" name="" class="form-control form-control-md" id="control_COLUMN4" placeholder="Full name" aria-describedby="nameTextFieldError" required="">
        <span id="nameTextFieldError" class="error" style="display: none;"><svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Please enter your name</span>
      </div>
      <div class="clearfix"></div>
    </div>
    <div class="form-group">
      <label for="control_EMAIL" class="col-sm-4">Email address*</label>
      <div class="col-sm-6 has-feedback">
        <input type="email" name="" class="form-control form-control-md" id="control_EMAIL" placeholder="Email address" aria-describedby="emailError" value="" required="">
        <span id="emailError" class="error" style="display: none;"><svg class="icon"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#warning"></use></svg> Email address is mandatory</span>
      </div>
      <div class="clearfix"></div>
    </div>
</form>