如何在native native中验证TextInput值?

时间:2016-03-22 06:07:02

标签: javascript android validation reactjs react-native

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

enter image description here

2 个答案:

答案 0 :(得分:48)

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

正则表达式功能

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
}

答案 1 :(得分:0)

您可以使用 def questionSearch(request): if request.method == 'GET': question_title = request.GET.get('title',) question_tag = request.GET.get('tag',) url = 'https://api.stackexchange.com/' + f'2.2/search/advanced? order=desc&sort=activity&q={question_title}&accepted=False&answers=1&tagged= {question_tag}&site=stackoverflow' resp = requests.get(url) resp = resp.json() questions = [] question_data = {} for i in resp["items"]: if i["is_answered"]: question_data = { 'question_url': i["link"], 'question_title': i["title"], 'question_tag': i["tags"], 'question_view_count': i["view_count"], 'question_answer_count': i["answer_count"] } questions.append(question_data) total = [] for item in questions: for key in item.keys(): total.append(item[key]) question = Question() for i in total: question.question_url = i question.question_title = i question.question_tag = i question.question_view_count = i question.question_answer_count = i question.save() context = { 'questions': questions, } return render(request, 'stackAPI/index.html', context) 上的onBlur事件来验证输入值 您可以在此事件上应用正则表达式或检查条件。

赞:

TextInput

就您而言, 正则表达式功能:

<TextInput
      onBlur= () => {
        //Conditions or Regex
      }
/>

文本输入代码:

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