简体   繁体   English

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

[英]How do I check to see is password matches confirm password before user is allowed to submit the form JS

Basically I have a sign-up form, I want the password and confirm password field to match before the user is allowed to submit the form.基本上我有一个注册表单,我希望密码和确认密码字段在允许用户提交表单之前匹配。 I have the password and confirm password matching logic but do not know how to disable user from submitting if they do not match我有密码并确认密码匹配逻辑,但不知道如果不匹配,如何禁止用户提交

This is my form in my html这是我在 html 中的表格

<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>

This is my logic to compare the password field and confirm password field on keyup这是我在 keyup 上比较密码字段和确认密码字段的逻辑

// 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()
})

How do I do it in a way that if passwords do not match user cannot submit the form.如果密码不匹配用户无法提交表单,我该如何做到这一点。

A HTML form element can take use of a special comparison function before submitting by populating its onsubmit attribute. HTML 表单元素可以在通过填充其onsubmit属性提交之前使用特殊的比较 function。 That means the novalidate attribute must not be present.这意味着novalidate属性不能存在。

Your comparePwd() function just needs a little tweak: it needs to return false , in case something is wrong - eg the passwords do not match.您的comparePwd() function 只需要稍作调整:它需要返回false ,以防出现问题 - 例如密码不匹配。

So simply change the form to this:所以只需将表单更改为:

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

and the comparison function to this:并将 function 与此进行比较:

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'
}
}

Add this to your code将此添加到您的代码中

let submitBtn = document.getElementById("submitBtn");

and add id to the button to make it match:并将 id 添加到按钮以使其匹配:

<button class="confirm-pwd" type="submit" class="btn  mt-3 submit-btn" id="submitBtn">Sign-up</button>

This way you can disable the button when the 2 fields do not match这样,当 2 个字段不匹配时,您可以禁用按钮

if (pwd.value != confirmPwd.value) {
       matchingTxt.style.display = 'block'
       matchingTxt.style.color = 'red'
       matchingTxt.innerHTML = 'Not Matching'
       e.preventDefault()
       submitBtn.disabled = true
    } else {
        matchingTxt.style.display = 'block'
        matchingTxt.style.color = 'green'
        matchingTxt.innerHTML = 'Matching'
        submotBtn.disabled = false
    }

Call your function comparePwd() on click on submit button:点击提交按钮调用您的 function comparePwd():

<button onclick="comparePwd()" class="confirm-pwd" type="submit" class="btn  mt-3 submit-btn">Sign-up</button>

after adding this onclick action to button, remove the event listeners both将此 onclick 操作添加到按钮后,删除事件侦听器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM