ReactFix
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • React
  • React Native
  • Programming
  • Object Oriented Programming

Friday, November 25, 2022

[FIXED] How to Block Special Character in react?

 November 25, 2022     reactjs   

Issue

Is there any samples , In blocking special character on key press or onblur event for react textArea box.

Since reactTextArea does have a call onClick event which calls the function to process the request in post method.


Solution

You could use a regular expression for special characters and replace them all with an empty string.

Example

class App extends React.Component {
  state = { value: "" };

  onChange = event => {
    this.setState({ value: event.target.value.replace(/[^\w\s]/gi, "") });
  };

  render() {
    return <input value={this.state.value} onChange={this.onChange} />;
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>



Answered By - Tholle
Answer Checked By - - Cary Denson (ReactFix Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post Home

Featured Post

Is Learning React Difficult?

React is difficult to learn, no ifs and buts. React isn't inherently difficult to learn. It's a framework that's different from ...

Total Pageviews

Copyright © ReactFix