0

I have input box, I don't want user to copy paste into the input box '-'(minus) , '.'(decimal) value

Abhishek
  • 231
  • 2
  • 12

2 Answers2

1

You can use onCopy, onPaste, onCut event to disable action.

https://reactjs.org/docs/events.html#keyboard-events

 const handleChange = (e) => {
    e.preventDefault();
  };

<TextField
      value={val}
      onCut={handleChange}
      onCopy={handleChange}
      onPaste={handleChange}
/>
Rahul Sharma
  • 8,373
  • 1
  • 11
  • 35
  • can i ask for some help on this? https://stackoverflow.com/questions/65423577/how-to-get-the-values-of-checkbox-instead-of-true – Kunal Vijan Dec 24 '20 at 06:13
1

below an implementation blocking paste with given conditions:

export default function App() {
  const [value, setValue] = useState('')

  const onPaste = (e) => {
    const paste = e.clipboardData.getData('text/plain')
    if (paste.match(/[-\.]/)) return
    setValue(paste)
  }

  return (
    <div>
      <input value={value} onPaste={onPaste} />
      {value}
    </div>
  );
}
buzatto
  • 8,091
  • 5
  • 19
  • 24