<form id="password-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" id="submit-btn">确认修改</button></form><script> function checkNewPassword() { const password = document.getElementById('new-password').value; const regex = /^(?=.[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 = '两次输入的密码不相同'; } } const form = document.getElementById('password-form'); const submitBtn = document.getElementById('submit-btn'); form.addEventListener('submit', function(event) { event.preventDefault(); // 阻止默认的表单提交行为 const currentPassword = document.getElementById('current-password').value; const newPassword = document.getElementById('new-password').value; const confirmPassword = document.getElementById('confirm-password').value; // 发送 AJAX 请求到后台 const xhr = new XMLHttpRequest(); xhr.open('POST', '/change-password'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { alert('密码修改成功!'); } else { alert('密码修改失败,请重试!'); } } }; xhr.send(JSON.stringify({ currentPassword: currentPassword, newPassword: newPassword, confirmPassword: confirmPassword })); // 禁用提交按钮,防止重复提交 submitBtn.disabled = true; });</script>
时间: 2024-04-05 12:32:54 浏览: 148
这段代码是一个修改密码的表单,包括当前密码、新密码和确认密码三个输入框,以及一个提交按钮。在用户输入新密码和确认密码时,会进行格式和一致性检查,并在下方显示提示信息。当用户点击提交按钮时,会发送一个 AJAX 请求到后台,将当前密码、新密码和确认密码作为参数传递给服务器,如果修改成功则弹出提示框,否则也会弹出提示框。同时,为了防止用户重复提交,提交按钮会在点击后被禁用。
相关问题
<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="请输入新密码" pattern=".{6,}" title="密码长度必须大于等于6位" required> <div class="invalid-feedback">密码长度必须大于等于6位</div> </div> <div class="form-group"> <label for="confirm-password">确认密码</label> <input type="password" class="form-control" id="confirm-password" name="confirm-password" placeholder="请再次输入新密码" pattern=".{6,}" title="密码长度必须大于等于6位" required> <div class="invalid-feedback">密码长度必须大于等于6位</div> </div> <button type="submit" class="btn btn-primary">确认修改</button> </form>
这段代码是一个包含三个密码输入框的表单,分别是“当前密码”、“新密码”和“确认密码”。其中,“新密码”和“确认密码”输入框使用了`pattern`属性,规定了密码的最小长度为6位,并给出相应的提示信息。此外,“当前密码”输入框使用了`required`属性,表示该输入框必须填写才能提交表单。
如果要修改为必须有大小写字母加特殊字符组成,且不小于8位,则可以将代码修改为:
```html
<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>
```
这段代码与之前的代码相比,主要是对“新密码”和“确认密码”输入框的校验逻辑进行了修改。在输入框中添加了`oninput`事件,当用户输入密码时,会自动调用`checkNewPassword`和`checkConfirmPassword`函数进行校验。`checkNewPassword`函数的逻辑与前面的前端表单中的校验逻辑相同,都是使用正则表达式判断密码是否符合要求,并在提示信息中显示具体的错误信息。`checkConfirmPassword`函数则用于判断两次输入的密码是否相同,并在提示信息中显示错误信息。
这样修改后,用户必须输入符合要求的新密码才能修改密码,并且两次输入的密码必须相同才能提交表单。
请把刚才未写完的代码继续展示出来,从“<div class="col-sm-6”这一段开始
<div class="col-sm-6">
<div class="form-group row">
<label for="email" class="col-sm-4 col-form-label">{{ __('Email Address') }}</label>
<div class="col-sm-8">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-4 col-form-label">{{ __('Password') }}</label>
<div class="col-sm-8">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<div class="col-sm-8 offset-sm-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-sm-8 offset-sm-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
@if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
@endif
</div>
</div>
</div>
阅读全文