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

150
Vistas
How to change input.value to capitalize before submitting to array Javascript

I have a button that submits a input.value to the empty array.

The function is checking if there isnt' any matching names inside the array and throws the error if there is, but not if there are lowercase or uppercase letters.

I want this function to capitalize the input.value first letter, and lowercase the rest before submitting it into the array. So I want to easly check if there won't be two same names just written the other way.

Do you know how to make it work using vanilla JS?

const nameInput = document.getElementById('inputName');
let nameListDisplay = document.getElementById('nameListDisplay');
let assignmentDisplay = document.getElementById('assignmentDisplay');
let onlyLetters = '^[a-zżźćńłśąęóA-ZŻŹĆŃŁŚĄĘÓ ]+$';

let namesList = [];
    
function addName() {
        if (nameInput.value.match(onlyLetters)) {
            if (namesList.includes(nameInput.value)) {
                console.log('error');
            }
            else {
                console.log('name added');
                namesList.push(nameInput.value);
                nameInput.value = ''; 
                displayNames();
            }  
        }
        else {
            console.log('error');
        }
    };
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This function will capitalize first letter and lower case the rest of the string.

function toTitleCase(str) {
  return str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
}

and then:

if (nameInput.value.match(onlyLetters)) {
            if (namesList.includes(toTitleCase(nameInput.value))) { // here
                console.log('name already exists');
            }
            else {
                console.log('name added');
                namesList.push(toTitleCase(nameInput.value)); // here
                nameInput.value = ''; 
                displayNames();
            }  
        }
        else {
            console.log('error');
        }
}
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