<form> <div class="form-group"> <label for="current-password">当前密码</label> <input type="password" class="form-control" id="current-password" name="current-password" placeholder="请输入当前密码" required> <div class="invalid-feedback">请输入当前密码</div> </div> <div class="form-group"> <label for="new-password">新密码</label> <input type="password" class="form-control" id="new-password" name="new-password" placeholder="请输入新密码" required oninput="checkNewPassword()"> <span id="new-password-tip" style="color:red;"></span> </div> <div class="form-group"> <label for="confirm-password">确认密码</label> <input type="password" class="form-control" id="confirm-password" name="confirm-password" placeholder="请再次输入新密码" required oninput="checkConfirmPassword()"> <span id="confirm-password-tip" style="color:red;"></span> </div> <button type="submit" class="btn btn-primary">确认修改</button></form><script>function checkNewPassword() { const password = document.getElementById('new-password').value; const regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]).{8,}$/; const isValid = regex.test(password); const tip = document.getElementById('new-password-tip'); if (isValid) { tip.innerHTML = ''; } else { tip.innerHTML = '密码必须包含大小写字母、数字和特殊字符,并且长度不小于8位'; }}function checkConfirmPassword() { const password = document.getElementById('new-password').value; const confirmPassword = document.getElementById('confirm-password').value; const tip = document.getElementById('confirm-password-tip'); if (password === confirmPassword) { tip.innerHTML = ''; } else { tip.innerHTML = '两次输入的密码不相同'; }}</script>
时间: 2023-10-17 19:05:17 浏览: 68
html表单<form></form>
这是一个包含当前密码、新密码和确认密码的表单,其中新密码和确认密码都加入了密码校验的功能。具体来说,新密码和确认密码都需要包含大小写字母、数字和特殊字符,并且长度不小于8位。同时,在确认密码输入框中,加入了实时校验,如果输入的确认密码与新密码不一致,则会提示“两次输入的密码不相同”。这样可以提高用户的密码安全性,避免用户输入弱密码或者错误的确认密码。
阅读全文