javascriptreactjsjsxdom-events

How to prevent user from entering spaces in an input form field in ReactJS?


I have created a component called QueryForm to be used for search functionality. I am returning in that component this:


Solution

  • You can replace the spaces to empty string while setting the value:

    const onChange = (event, { newValue }) => {
      setInputValue(newValue.replace(/\s/g, ''));
    };