CSS / JQuery表单验证

时间:2017-06-08 02:26:27

标签: jquery css

如何在密码输入字段中实现此类验证。 这可以通过模态完成吗?谢谢你的帮助!

enter image description here

1 个答案:

答案 0 :(得分:0)

这包含一个类似的密码验证框。您可以在JavaScript中提取if语句以真正验证密码。

$('input[type=password]').focusin(function () {
  $('div.pw-validator').fadeIn(300);
});

$('input[type=password]').focusout(function () {
  $('div.pw-validator').fadeOut(300);
});

$('input[type=password]').keyup(function () {
  var pw = $(this).val();
  
  if (pw.length >= 8)
    $('.pw-validator span.character-count').addClass('accomplished');
  else
    $('.pw-validator span.character-count').removeClass('accomplished');
  
  var hasLowercase = false;
  var hasUppercase = false;
  
  for (var i = 0; i< pw.length; i++) {
    if (pw[i] >= 'a' && pw[i] <= 'z')
      hasLowercase = true;
    else if (pw[i] >= 'A' && pw[i] <= 'Z')
      hasUppercase = true;
  }
  if (hasUppercase)
    $('.pw-validator span.uppercase-character').addClass('accomplished');
  else
    $('.pw-validator span.uppercase-character').removeClass('accomplished');
  
  if (hasLowercase)
    $('.pw-validator span.lowercase-character').addClass('accomplished');
  else
    $('.pw-validator span.lowercase-character').removeClass('accomplished');
  
});
input {
  display: block;
  border: 1px solid #aaa;
  border-radius: 3px;
  padding: 5px;
  margin-bottom: 20px;
}

div.pw-validator {
  display: none;
  width: 150px;
  height: 100px;
  box-shadow: 0px 0px 5px black;
  position: absolute;
  top: 80px;
  left: 20px;
}
div.pw-validator span {
  margin: 5px;
  color: black;
  font-family: Arial;
  font-size: 12px;
  display: block;
  transition: color 0.2s ease-in-out;
}
div.pw-validator span.accomplished {
  color: #73ac21;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="Username" class="username"/>
<input type="password" placeholder="Password" class="password"/>
<div class="pw-validator">
  <span class="character-count">8 or more characters</span>
  <span class="uppercase-character">1 uppercase character</span>
  <span class="lowercase-character">1 lowercase character</span>

</div>