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

87
Visualizações
How can I limit the input tag to only 10 Commas?

You need an input custom that limits you to 10 commas.

When it is 10, it should be possible to modify it.

Currently, the input event works faster, so additional commas are added so it cannot be edited.

my code

// input props
value={relationSearch}
onChange={(e) => handleOnChangeInput(e)}
onKeyPress={handleKeyPress}

//callbackfunction
const relationCallbackFunction = {
  handleOnChangeInput: (e) => {
    setRelationSearch(e.target.value);
  },
};

// input event area
const handleOnChangeInput = (e) => {
  relationCallbackFunction.handleOnChangeInput(e);
};

const handleKeyPress = (e) => {
  const currentStr = relationSearch.split(',');
  console.log(e.target.value);

  if (currentStr.length > 11) {
    e.target.value ='';
  }
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you could do this a couple of ways, but here is a simple solution. The important part here is to ensure you set your input to a controlled input, where you provide the value from react.

We need a function which will implement your logic. It should take in a string and the total number of commas you want, and limit that string to the comma amount.

Below is a very simple function that does this. It splits the string using commas, ensures the result array stays at 10 length, and returns a joined string from the resultant array.

function ensureCommas(str, commas = 10) {
 const commaArray = str.split(',');

 const reduced = commaArray.slice(0, commas);
 
 return reduced.join(',');

}

Now to use it. Here is a very simple App component which keeps the input value in state and provides this state value to the input, and has an onChange event handler which calls the above function on every key press

import { useState } from "react";
import "./styles.css";

function ensureCommas(str, commas = 10) {
  const commaArray = str.split(",");

  return commaArray.slice(0, 10);
}

    export default function App() {
      const [value, setValue] = useState("");
    
      const onInputChange = (e) => {
        const inputVal = e.target.value;
    
        const newInputVal= ensurecommas(inputVal , 10);
    
        setValue(newInputVal);
      };
    
      return (
        <div className="App">
          <input value={value} onChange={onInputChange}></input>
        </div>
      );
    }

CodeSandbox

about 4 years ago · Juan Pablo Isaza Relatório

0

Remove the handleKeyPress function from the input props and update the relationCallbackFunction object.

 const relationCallbackFunction = {
  handleOnChangeInput: (e) => {
    let value = e.target.value;
    const currentStr = value.split(",");
    if (currentStr.length <= 10) {
      setRelationSearch(value);
    }
  }
};
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