Home > Back-end >  How to send form after checking its data?
How to send form after checking its data?

Time:04-08

I'm trying to check form's data before its data are submitted. I've already done all the control but I don't know how to submit the form. I tried to search something but almost all require jQuery, but I would not use it. Could you help me please?

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);
}

CodePudding user response:

e.preventDefault(); stops the form from submitting, so don't do that if you want it to submit.

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

CodePudding user response:

Use .submit()

It'll look something like:

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

});

  • Related