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

140
Views
HTML js how to check if name exists in list ignoring case and to stop form submission

I have a form with an input field for the username to check if the field is empty and to check if what the user entered exists on a list.

This half works as in the messages appear when invalid however the form still submits if the user enters a name that exists in the list where as the form does not submit when the field is blank (but this is due to the input required tag being present).

Also if the user enters a name that exists in the list but with different case letters the message 'The name already exists' does not appear as it does not match the case of the name in the list.

Please see my code below:

<script>
function usernameFunction() {
  let x = document.getElementsByName("username")[0].value;
  let listNames = ["john", "sid", "paul", "jim"];

  let text;
  text = "";
  if (x === '' ||  x == null) {
    text = "Username cannot be blank";
  }
    for (let i = 0; i < listNames.length; i++) {
    if (listNames[i] === x) {
      text = ('The name already exists')
    }
  }

  document.getElementById("username_errors").innerHTML = text;
}
document.addEventListener('invalid', (function () {
  return function (e) {
    e.preventDefault();

    document.getElementsByName("username").focus();

  };
})(), true);
</script>

<form method="POST" action="">
<input type="text" name="username"  placeholder="username" name class="input_fields" required>
<div class="error-message" id="username_errors"></div>
      <input class="save_btn" type="submit" value="Save" name="save_username" onclick="usernameFunction()">
</form>

How can I adapt my code to stop submission of the form when the name exists in the list and ignoring the case? - for example - john being in the list if the user enters John, I would like the message 'The name already exists' to still appear and to not submit.

Thanks in advance

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

0

This would be the line for you:

 if ( listNames.includes(x.toLowerCase()) ) {
    return false;
  }

It check if the value John is included in the array of names. And the String Function toLowerCase() make morph the name to lowercase.

UPDATE

function usernameFunction(e) {
  e.preventDefault();
  
  let text = '';
  let x = document.getElementsByName("username")[0].value;
  let listNames = ["john", "sid", "paul", "jim"];
  if ( listNames.includes(x.toLowerCase()) ) {
    text = x + ' allready taken.';    
  }
  if (x === '' ||  x == null) {
    text = "Username cannot be blank";
  }
  
  if (text.length > 0) {
    document.getElementById("username_errors").innerHTML = text;  
    return false;
  }
  // trigger Submit programmatically
  document.getElementById("myForm").submit(); 
  console.log('trigger submit')  
}

document.addEventListener('invalid', (function () {
  return function (e) {
    e.preventDefault();

    document.getElementsByName("username").focus();

  };
})(), true);
<form method="POST" id="myForm">
<input type="text" name="username" value="John" placeholder="username" name class="input_fields" required>
<div class="error-message" id="username_errors"></div>
<button class="save_btn" onclick="usernameFunction(event)">SUBMIT</button>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

You can use toUpperCase() for this.

if (listNames[i].toUpperCase() === x.toUpperCase())
 {
  text = ('The name already exists')
}
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!