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

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

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 Denunciar

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 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