2

I've see a lot of solution and questions similar to this but none of them helped me out

here is a simple html form with JavaScript validation

log error message on key up when the value of input fields are less than required length (less than 5)

log error message on submit when input fields are empty (first Name and last Name)

problem is when I click submit button form is submitted regardless of errors

I have tried preventDefault() and return false but none of them worked out

how can I improve the code to make them work correctly? for both key up and submit

let fname = document.getElementById('fname');
let lname = document.getElementById('lname');
form = document.querySelector('#myForm');

function checkFirstName() {
  let valid = true;
  if (fname.value.length < 5) {
    console.log('first name must be greater than 5');
    valid = false;
  }
}

function checkLastName() {
  let valid = true;
  if (lname.value.length < 5) {
    console.log('last name must be greater than 5');
    valid = false;
  }
}
form.addEventListener('input', function(e) {
  switch (e.target.id) {
    case 'fname':
      checkFirstName();
      break;
    case 'lname':
      checkLastName();
      break;
  }
});

form.addEventListener('submit', function(e) {
  let isFormField = true;
  if (fname.value === '') {
    console.log('first name is required')
    isFormField = false;
  }
  if (lname.value === '') {
    console.log('last name is required')
    isFormField = false;
  }
  if (!isFormField) {
    e.preventDefault();
    return
  }
});
<form method="get" id="myForm">
  <div>
    <input type="text" class="form-control" id="fname" name="fname"></br>
    <small class="small" id="small"></small>
  </div>
  <div>
    <input type="text" class="form-control" id="lname" name="lname"></br>
    <small class="small" id="small"></small>
  </div>
  <button type="submit" name="send">submit</button>
</form>

5
  • There's a "Tidy" button in the snippet editor. Please use it before you add the snippet to your question. Commented Sep 4, 2021 at 14:33
  • It's <br /> and not </br> Commented Sep 4, 2021 at 14:34
  • You are only calling checkFirstName and checkLastName on input event. If user submit form then you are not validating the inputs. Commented Sep 4, 2021 at 14:38
  • @HR01M8055 when I use it outside the if condition it stops the from submission even if there is no errors Commented Sep 4, 2021 at 14:39
  • @HR01M8055 can u pls demonstrate it ? Commented Sep 4, 2021 at 14:41

1 Answer 1

1

I've moved all the validation of firstname in the function checkFirstName and lastname in checkLastName. Just to be concise

You can make it separate, But you have to remember that form will only submit if checkFirstName and checkLastName returns true

let fname = document.getElementById('fname');
let lname = document.getElementById('lname');
form = document.querySelector('#myForm');

function checkFirstName() {
  if (fname.value.length >= 5) return true;
  if (fname.value === "") {
    console.log('Last name is required')
    return false
  }
  console.log('first name must be greater than 5');
  return false;
}

function checkLastName() {
  if (lname.value.length >= 5) return true;
  if (lname.value === "") {
    console.log('Last name is required')
    return false
  }
  console.log('last name must be greater than 5');
  return false;
}
form.addEventListener('input', function(e) {
  switch (e.target.id) {
    case 'fname':
      checkFirstName();
      break;
    case 'lname':
      checkLastName();
      break;
  }
});

form.addEventListener('submit', function(e) {
  if (!checkFirstName() || !checkLastName()) {
    e.preventDefault();
    return
  }
});
<form method="get" id="myForm">
  <div>
    <input type="text" class="form-control" id="fname" name="fname"></br>
    <small class="small" id="small"></small>
  </div>
  <div>
    <input type="text" class="form-control" id="lname" name="lname"></br>
    <small class="small" id="small"></small>
  </div>
  <button type="submit" name="send">submit</button>
</form>

Sign up to request clarification or add additional context in comments.

5 Comments

can you write the same code and put e.preventDefault(); outside the if condition?
This code also works, What's wrong with this?
nothing wrong you asked me why I use it inside if condition, I thought you have better solution of using it outside if condition
I apologize for that, I was thinking something wrong. But this solution will work for you.
Would you mind accepting my answer if it solved your issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.