如何在native native中验证TextInput值? [英] How to validate TextInput values in react native?

查看:82
本文介绍了如何在native native中验证TextInput值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在TextInput中输入电子邮件时,它应验证并显示错误消息。输入的电子邮件是否有效

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

推荐答案

您可以使用正则表达式检查输入的邮件是否有效。

You can use a regex to check if the mail entered is valid.

正则表达式功能

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);
};

提交文字输入功能

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

这篇关于如何在native native中验证TextInput值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆