javascriptformsvalidationsubmitsubmission

I can't link validation and form submission. Need help setting the variable (true or false) so that an empty form is not sent?


const form = document.querySelector('.form__contact');
const name = document.querySelector('input[name=name]');
const email = document.querySelector('input[name=email]');
const btnSubmit = document.querySelector('.btn-submit')

two-field validation function

function checkInputs() {
    let nameValue = name.value.trim();
    let emailValue = email.value.trim();

    if (nameValue === '') {
        setErrorFor(name, 'Name cannot be blank');

    } else if (!isName(emailValue)) {
        setErrorFor(name, 'Not a valid name');
    } else {
        setSuccessFor(name);
    }
    if (emailValue === '') {
        setErrorFor(email, 'Email cannot be blank');

    } else if (!isEmail(emailValue)) {
        setErrorFor(email, 'Not a valid email');

    } else {
        setSuccessFor(email);
    }
}
function setErrorFor(input, message) {
    const inputBox = input.parentElement;
    const errorMessage = inputBox.querySelector('.errors');
    inputBox.className = 'form__field error';
    errorMessage.textContent = message;
}
function setSuccessFor(input) {
    const inputBox = input.parentElement;
    const errorMessage = inputBox.querySelector('.errors');
    inputBox.className = 'form__field success';
    errorMessage.textContent = "";
}

field verification functions

function isName(name) {
    let regExpName = /^[]{}?/;
    return regExpName.test(name);
}
function isEmail(email) {
    let regExpEmail = /[])?/;
    return regExpEmail.test(email);
}

serialize-this function generates a string to send to the server. The function is ready, large, so I didn't throw it all in the post

function serialize(form) {
    if (!form || form.nodeName !== "FORM") {
        return false;
    }
    -- ----   - - - - -
}

and submit itself



btnSubmit.addEventListener('click', event => {
    event.preventDefault();

Here, need "if else" or anything else for "const isValidate = false"

    checkInputs();
    console.log(serialize(form));

});

Solution

  • "return false" if validation error, Try this:

    function checkInputs() {
      let nameValue = name.value;
      let emailValue = email.value;
    
      if (!nameValue || nameValue.trim().length() === 0) {
        setErrorFor(name, "Name cannot be blank");
        return false; // return false
      } else if (!isName(emailValue)) {
        setErrorFor(name, "Not a valid name");
        return false; // return false
      } else {
        setSuccessFor(name);
      }
    
      if (!emailValue || emailValue.trim().length() === 0) {
        setErrorFor(email, "Email cannot be blank");
        return false; // return false
      } else if (!isEmail(emailValue)) {
        setErrorFor(email, "Not a valid email");
        return false; // return false
      } else {
        setSuccessFor(email);
      }
    
      return true; // default true
    }
    const isValidate = checkInputs();
    if (isValidate) {
      console.log(serialize(form));
    }