Reputation: 301
I have input box, I don't want user to copy paste into the input box '-'(minus) , '.'(decimal) value
Upvotes: 0
Views: 3966
Reputation: 10382
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>
);
}
Upvotes: 1
Reputation: 10071
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}
/>
Upvotes: 1