1

I have a TextInput for a specific format of e-mail address, and have a regex to validate it. I would like to validate it while the user types, with the exact value on the TextInput box, but I'm not getting that.

What I have done is actually add that validation to the OnChangeText method of the TextInput object. The mechanics seems to work, but it is validating against the content 1 letter behind the current content.

Ex.: If I have typed "June" it will validate "Jun" only. If I delete the "e", then it will get "June" and subsequently.

Is that correct, or is there somewhere else I can call my validation to have the expected effects?

<TextInput
    placeholder='Type in your email'
    style={Styles.Input}
    onChangeText={(text) => {this.setLogin(text)}}
    value={ this.state.login }
    autoCompleteType={"email"}
    autoFocus={true}
/>
...
...
...
setLogin = (input) => {
    this.setState({login: input});
    console.log(this.state.login_regex.test(this.state.login));
    if (!this.state.login_regex.test(this.state.login)) {
        this.setState({login_msg: "Use an internal mail address"});
    } else {
        this.setState({login_msg: ""});
    };
};   
1
  • What's output of console.log(this.state.login_regex.test(this.state.login))? And can you share login_regex state? Commented Nov 17, 2019 at 23:25

2 Answers 2

1

setState is an asynchronous function, that‘s why this.state.login might still hold the old value when testing.

You can either use the input instead of the value of the state, like this

if (!this.state.login_regex.test(input)) {
  ...
}

or you can pass a callback function as the second parameter to this.setState like this

this.setState({login: input}, () => {
  if (!this.state.login_regex.test(this.state.login)) {
    ...
  }
});

but I‘d usually recommend the first approach.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, it's actually in your setLogin function.

setLogin = (input)=>{
    this.setState({login: input}); // <= this is the problem
    console.log(this.state.login_regex.test(this.state.login));
    if (!this.state.login_regex.test(this.state.login)) {
        this.setState({login_msg: "Use an internal mail address"});
    } else {
        this.setState({login_msg: ""});
    };
};

In your code, you have called setState() right before your validation, which means you save your input before you check it. To resolve this, just remove the this.setState({login: input}); and your code will work properly.

P/s: Since React Native has to tranfer your datas and commands between JS side and native side, there might be some unexpected visual performances, e.g: your last input character will appear for a very short moment before disappearing.

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.