如何做到在android的表单验证 [英] how to do form validation in android

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

问题描述

我有我需要验证之前提交报名表。形式有以下字段:姓名,电子邮件,联系号码和密码。我需要的名字有一个值,该邮件有正确的格式,联络号码应该是数字至少为10个号码,密码至少为6个字符。

I have a registration form which I need to validate before submit. The form has the following fields:name,email, contact number and password. I need the name to have a value, the email to have the correct format,contact number should be numbers at least 10 numbers and the password to be at least 6 characters.

推荐答案

试试这个

vUsername = etUsername.getText().toString();
vFirstname = etFirstname.getText().toString();
vEmail = etEmail.getText().toString(); 
vPwd = etPwd.getText().toString();
vCpwd = etCpwd.getText().toString();   

if("".equalsIgnoreCase(vUsername) //vUsername.equalsIgnoreCase("") could lead to NPE
   || "".equalsIgnoreCase(vFirstname)
   || "".equalsIgnoreCase(vEmail)
   || "".equalsIgnoreCase(vPwd)
   || "".equalsIgnoreCase(vCpwd) )
{
    Toast.makeText(userRegistration.this, "All Fields Required.", 
         Toast.LENGTH_SHORT).show();
}
checkemail(vEmail);
if(emailcheck==true)
{
    // your code here
}

public void checkemail(String email)
{
    Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher matcher = pattern.matcher(email);
    emailcheck = matcher.matches();
}

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

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