5

I'm trying to prevent my TextInput from getting values like $,%,^,&,(,) etc. Basically my TextInput should allow letters only. My approach is as follows. But still i'm able to input these other characters. How can i prevent special characters from the TextInput

restrict(event) {
        const regex = new RegExp("^[a-zA-Z]+$");
        const key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        if (!regex.test(key)) {
            event.preventDefault();
            return false;
        }
    }

                         <TextInput
                                underlineColorAndroid='transparent'
                                allowFontScaling={false}
                                style={styles.questionText}
                                onKeyPress={e => this.restrict(e)}
                                value={firstNameState}
                            />
6
  • How can i detect special characters? Commented Dec 11, 2018 at 5:21
  • 2
    Try regex = /^[^!-\/:-@\[-`{-~]+$/; Commented Dec 11, 2018 at 6:07
  • @WiktorStribiżew Thanks it's working.One problem though, Special characters get deleted only after i'm inputting a character. So if I entered a special character in the beginning and not enter anything after, i end up having a special character in my TextInput. How can i handle this? Commented Dec 11, 2018 at 7:14
  • I am not sure why that happens. If it is still related to the regex and not the code, try /^[^!-\/:-@\[-`{-~]*$/ Commented Dec 11, 2018 at 7:52
  • did you solved it Commented Mar 5, 2019 at 10:30

3 Answers 3

1

the onKeyPress event on android does not work very well.

That is why I have chosen to use a method that eliminates these characters and then save it wherever you want, just as it might change the state of your field.

restrict = text => text.replace(/[`~0-9!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '')
Sign up to request clarification or add additional context in comments.

2 Comments

This should not be working unless you remove " i " from " gi " at the end of this expression
Could be just restrict = text => text.replace(/[^a-z]/gi, '').
0

I have to block special characters by this line of code.

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
 
if(format.test(string)){ 

   //Do something here

}

Comments

-1

You may define your OnChange event handler using your regex, where you will check if the input string matches your regex with /^[^!-\/:-@\[-`{-~]+$/.test(text):

const {useState} = React;

const App = () => {
  const [value, setValue] = useState("");
  const onChange = e => {
    const input = e.currentTarget.value;
    if (/^[^!-\/:-@\[-`{-~]+$/.test(input) || input === "") {
      setValue(input);
    }
  };
  return (
    <div className="App">
      <input
        value={value}
        onChange={onChange}
        underlineColorAndroid='transparent'
        allowFontScaling={false}
      />
    </div>
  );
}
ReactDOM.render(<App />, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>

1 Comment

OP said ReactNative, not React

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.