Android Studio 实现注册信息表单验证的源代码(实现账号,密码,邮箱,手机号验证)
发布日期:2021-06-29 15:03:49 浏览次数:4 分类:技术文章

本文共 3626 字,大约阅读时间需要 12 分钟。

运行样式

一、界面布局

1、界面样子:

在这里插入图片描述

2、在activity_main.xml当中创建布局界面

在这里插入图片描述

3、源代码

二、Java源代码

在这里插入图片描述

package com.example.application22;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;import java.util.regex.Matcher;import java.util.regex.Pattern;public class MainActivity extends AppCompatActivity {
EditText name = null; EditText password = null; EditText email = null; EditText phone = null; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = findViewById(R.id.name); password = findViewById(R.id.password); email = findViewById(R.id.email); phone = findViewById(R.id.phone); } public void register(View v){
String namestring = name.getText().toString(); String passwordstring = password.getText().toString(); String emailstring = email.getText().toString(); String phonestring = phone.getText().toString(); //判断账号 if(namestring.length() == 0 ){
Toast.makeText(getApplicationContext(),"账号不能为空",Toast.LENGTH_SHORT).show(); return; } if(namestring.length() > 16 ){
Toast.makeText(getApplicationContext(),"账号必须小于16位",Toast.LENGTH_SHORT).show(); return; } if(namestring.length() != 0 && namestring.length() < 8 ){
Toast.makeText(getApplicationContext(),"账号必须大于8位",Toast.LENGTH_SHORT).show(); return; } //判断密码 if(passwordstring.length() == 0 ){
Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show(); return; } if(passwordstring.length() > 16 ){
Toast.makeText(getApplicationContext(),"密码必须小于16位",Toast.LENGTH_SHORT).show(); return; } if(passwordstring.length() != 0 && passwordstring.length() < 8 ){
Toast.makeText(getApplicationContext(),"密码必须大于8位",Toast.LENGTH_SHORT).show(); return; } //判断邮箱 if(emailstring.length() == 0 ){
Toast.makeText(getApplicationContext(),"邮箱不能为空",Toast.LENGTH_SHORT).show(); return; } String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern p; Matcher m; p = Pattern.compile(regEx1); m = p.matcher(emailstring); if (!m.matches()){
Toast.makeText(getApplicationContext(),"邮箱格式不正确",Toast.LENGTH_SHORT).show(); return; } //判断电话 if(phonestring.length() == 0 ){
Toast.makeText(getApplicationContext(),"电话不能为空",Toast.LENGTH_SHORT).show(); return; } Pattern p1 = Pattern.compile("^1[3,5,7,8,9][0-9]{9}$"); Matcher m1 = p1.matcher(phonestring); if(!m1.matches()){
Toast.makeText(getApplicationContext(),"电话格式不正确",Toast.LENGTH_SHORT).show(); return; } Toast.makeText(getApplicationContext(),"注册成功请登录",Toast.LENGTH_SHORT).show(); } public void reset(View v){
name.setText(""); password.setText(""); email.setText(""); phone.setText(""); }}

转载地址:https://code100.blog.csdn.net/article/details/115282164 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Android Studio 安卓手机上实现火柴人动画(Java源代码—Python)
下一篇:计算机二级C语言:大题程序修改题

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月22日 01时54分51秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章