Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
how to remove trailing and leading spaces from textbox input

if I have something like this:

  return array.map(item => (
<input type="text" value={item.name}> onChange ={(name) => changeItemName(name.target.value)}

where the changeItemName function is basically a function that will change the name attribute of the current item. How can I go about making sure that the name attribute wont have any trailing or leading spaces? I can do something like this in my changeItemName function:

changeItemName(name){
name.trim()
... logic to change name of item }

However since I am using onChange(), this method is called everytime a character is typed. This means that if a user types a regular space between 2 words, this wouldnt work because the name passed into the function would be trimmed thus now allowing any spaces. How can I tweak this so that spaces still work but leading and trailing white spaces are gone?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can trim the name when the user finished typing. For example when the input got out of focus. Try this:

<input type="text" value={item.name}>
    onChange ={(name) => changeItemName(name.target.value)}
    onBlur={()=> { item.name = item.name.trim() }} 
/>

Here the onBlur function would fire when user goes to some other input or other part of your website.

about 4 years ago · Juan Pablo Isaza Relatório

0

There are several options, one is to use a debouncing function that will trim the input after the user stops typing for a couple hundred milliseconds. The other option is to trim the input on the blur event.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda