How to validate TextInput values in react native?

后端 未结 2 1074
耶瑟儿~
耶瑟儿~ 2021-01-31 04:35

for example, While entering an email in the TextInput, it should validate and display the error message. where the entered email is valid or not

2条回答
  •  误落风尘
    2021-01-31 04:50

    You can validate your input value using onBlur event on TextInput You can apply your regex or check conditions on this event.

    Like this:

     {
            //Conditions or Regex
          }
    />
    

    In your case, Regex function:

    validateEmail = (email) => {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
    };
    

    Text Input Code:

     {
        if (!this.validateEmail(this.state.text_input_email)) {
           // not a valid email
        } else {
           // valid email
        }
      }
    />
    

提交回复
热议问题