Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda