Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
html javascript form no longer working since adding new input field

I have the following html and javascript code for validation on the input fields, this was working with the one input field for first name but since I tried to extend my code by adding a new input field for last name now the form validation has stopped working as follows:

function myFunction() {

  let x = document.getElementsByName("first_name").[0]value;
  let y = document.getElementsByName("last_name")[0].value;

  let text;
  text = "";
  
  if (x == '' ||  x == null) {
    text = "Input not valid";
  }
  document.getElementById("first_name_errors").innerHTML = text;
}

  if (y == '' ||  y == null) {
    text = "Input not valid";
  }
  document.getElementById("last_name_errors").innerHTML = text;
}


document.addEventListener('invalid', (function () {
  return function (e) {
    e.preventDefault();
    document.getElementsByName("first_name").focus();
    document.getElementsByName("last_name").focus();
  };
})(), true);

</head>
<body>

<input type="text" name="first_name"  placeholder="first name" name class="input_fields" required>
<div class="error-message" id="first_name_errors"></div>
      <input class="save_btn" type="submit" value="Save" name="save_fname" onclick="myFunction()">

<br><br>

<input type="text" name="last_name"  placeholder="last name" name class="input_fields" required>
<div class="error-message" id="last_name_errors"></div>
      <input class="save_btn" type="submit" value="Save" name="save_lname" onclick="myFunction()">

How can I get this back working with the extra input field last name added? Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

there are many errors here. you may find them by your own by debugging your console.log

  1. error, at let x = document.getElementsByName("first_name").[0]value;
  2. there are a to many }
  3. eventlisteners need to be on the input and shouldn't be inside the check function
  4. there are empty name attributes on your inputs

fixing it blind it would be something like:

let firstName = document.getElementsByName('first_name')[0];
let lastName = document.getElementsByName('last_name')[0];

function checkValid() {
  let x = firstName.value;
  let y = lastName.value;

  let text;
  text = '';

  if (x == '' || x == null) {
    text = 'Input not valid';
  }
  document.getElementById('first_name_errors').innerHTML = text;

  if (y == '' || y == null) {
    text = 'Input not valid';
  }
  document.getElementById('last_name_errors').innerHTML = text;
}

firstName.addEventListener('invalid', function () {
  firstName.focus();
});

lastName.addEventListener('invalid', function () {
  lastName.focus();
});
<input type="text" name="first_name"  placeholder="first name" class="input_fields" required>
<div class="error-message" id="first_name_errors"></div>
  
  <input class="save_btn" type="submit" value="Save" name="save_fname" onclick="checkValid()">

<br><br>

  <input type="text" name="last_name"  placeholder="last name" class="input_fields" required>
<div class="error-message" id="last_name_errors"></div>
  
  <input class="save_btn" type="submit" value="Save" name="save_lname" onclick="checkValid()">
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!