I have an input box, I don't want users to copy paste the following into the input box:
- (minus) and . (decimal) value.
2 Answers
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>
);
}
Comments
You can use onCopy, onPaste and onCut events 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}
/>
123-4, input value will be1234, or block the paste altogether?