检查数据后如何发送表单?

How to send form after checking its data?

我正在尝试在提交数据之前检查表单数据。我已经完成了所有的控制,但我不知道如何提交表单。我试图搜索一些东西,但几乎都需要 jQuery,但我不会使用它。你能帮帮我吗?

form.forEach(e => {
    e.addEventListener("submit", (e) => {
        e.preventDefault();
        if(checkInputs())
            //send the fomr's data. But how?
    })
});

function checkInputs(){
    if(containerForm[1].classList.contains('hide')){
        const username = document.getElementById("usernameSignIn");
        const password = document.getElementById("passwordSignIn");
        const usernameValue = username.value.trim(); 
        const passwordValue = password.value;
        let correctUser, correctPass = false;

        if(username.value === '' || username.value === null){
            printError(username, "Username cannot be blank");
        } else {
            printSucces(username);
            correctUser = true;
        }

        if(passwordValue === '' || passwordValue === null){
            printError(password, "Password cannot be blank");
        } else if(passwordValue.length < 8 || !passwordCheck(passwordValue)){
            printError(password, "Incorrect password");
        } 

        if(passwordCheck(passwordValue)){
            printSucces(password);
            correctPass = true;
        }

        return correctUser && correctPass ? true : false;
    }
}

function printError(input, error){
    input.parentElement.classList = "row error";
    input.parentElement.querySelector('.errorMessage').innerText = error;
}

function printSucces(input){
    input.parentElement.classList = "row success";
    input.parentElement.querySelector('.errorMessage').innerText = null;
}

function passwordCheck(password){
    let regex = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()+=-\?;,./{}|\":<>\[\]\\' ~_]).{8,}/;
    return regex.test(password);
}

使用.submit()

它看起来像:

form.forEach(e => {
e.addEventListener("submit", (e) => {
    e.preventDefault();
    if(checkInputs())
        e.target.submit();
})

});

e.preventDefault(); 会阻止表单提交,因此如果您希望提交表单,不要 这样做。

e.addEventListener("submit", (e) => {
    if (!checkInputs())
        e.preventDefault();
});