已为小白,啥都不懂 2022-01-14 11:04 采纳率: 98%
浏览 24
已结题

在允许用户提交表单 JS 之前,我如何检查密码是否与确认密码匹配

我有一个注册表单,我希望密码和确认密码字段在允许用户提交表单之前匹配。我有密码并确认密码匹配逻辑,但不知道如果不匹配,如何禁止用户提交
这是我在我的 html 中的表单


```javascript


<form action="/register" method="post" novalidate class="mt-4" class="form">
                        <div class="mb-3">
                        <label for="username" class="form-label">Company Name*</label>
                        <input type="text" class="form-control" required id="username" name="username"
                            placeholder="Facebook Ltd">
                        </div>
                        <div class="mb-3">
                            <label for="exampleInputEmail1" class="form-label">Email address*</label>
                            <input type="email" class="form-control" required id="exampleInputEmail1" aria-describedby="emailHelp"
                                placeholder="John@gmail.com" name="email">
                            <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label">Password*</label>
                            <input type="password" class="form-control password" required id="password
                                placeholder="Min 8 Characters" name="password">
                        </div>
                        <div class="mb-3">
                            <label for="confirm-password" class="form-label">Confirm Password*</label>
                            <input type="password" class="form-control confirm-password" required id="confirm-password" placeholder="Must Match">
                            <span class="matching-txt mt-1">Not Matching</span>
                        </div>
                        <button class="confirm-pwd" type="submit" class="btn  mt-3 submit-btn">Sign-up</button>
                    </form>

这是我在 keyup 上比较密码字段和确认密码字段的逻辑


```javascript
// Password and Confirmed passwords validation
let pwd = document.querySelector('.password');
let confirmPwd = document.querySelector('.confirm-password')
let matchingTxt = document.querySelector('.matching-txt')
let form = document.querySelector('.form')
function comparePwd() {
    if (confirmPwd.value) {
    if (pwd.value != confirmPwd.value) {
       matchingTxt.style.display = 'block'
       matchingTxt.style.color = 'red'
       matchingTxt.innerHTML = 'Not Matching'
       e.preventDefault()

    } else {
        matchingTxt.style.display = 'block'
        matchingTxt.style.color = 'green'
        matchingTxt.innerHTML = 'Matching'
    }
} else {
    matchingTxt.style.display = 'none'
}
}

confirmPwd.addEventListener('keyup' , () => {
    comparePwd()
})

pwd.addEventListener('keyup' , () => {
    comparePwd()
})



如果密码不匹配用户无法提交表单,我该如何做到这一点。





  • 写回答

3条回答 默认 最新

  • 几何心凉 2022年度博客之星前端领域TOP 1 2022-01-14 11:17
    关注

    HTML 表单元素可以通过填充其onsubmit属性在提交之前使用特殊的比较功能。这意味着该novalidate属性不得存在。
    您的comparePwd()函数只需要稍作调整:它需要返回false,以防出现问题 - 例如密码不匹配。
    所以只需将表单更改为:

    <form action="/register" method="post" onsubmit="return comparePwd()" class="mt-4" class="form">
    
    

    以及对此的比较功能:

    function comparePwd() {
        if (confirmPwd.value) {
        if (pwd.value != confirmPwd.value) {
           matchingTxt.style.display = 'block'
           matchingTxt.style.color = 'red'
           matchingTxt.innerHTML = 'Not Matching'
           return false
           e.preventDefault()
    
        } else {
            matchingTxt.style.display = 'block'
            matchingTxt.style.color = 'green'
            matchingTxt.innerHTML = 'Matching'
        }
    } else {
        matchingTxt.style.display = 'none'
    }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月22日
  • 已采纳回答 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败