0

I have an input box, I don't want users to copy paste the following into the input box:
- (minus) and . (decimal) value.

2
  • do you want user when pasting something like 123-4, input value will be 1234, or block the paste altogether? Commented Dec 24, 2020 at 6:10
  • block the paste when it contains minus or decimal Commented Dec 24, 2020 at 6:12

2 Answers 2

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>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.