Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!