Android Studio 实现注册信息表单验证的源代码(实现账号,密码,邮箱,手机号验证)
发布日期:2021-06-29 15:03:49
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月26日 22时18分24秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2020年大学生电子设计竞赛,B题,单相在线式不间断电源,详细技术方案!
2019-04-29
大佬终于把鸿蒙OS讲明白了,收藏了!
2019-04-29
知乎热议:嵌入式开发中C++好用吗?
2019-04-29
这100道Linux常见面试题,看看你会多少?
2019-04-29
嵌入式开发中常用的几种通信接口总结
2019-04-29
什么?电路板上还要喷漆?
2019-04-29
读博读废了是种什么样的体验?
2019-04-29
2020年,技术圈十大“翻车”事件!
2019-04-29
C语言状态机编程思想
2019-04-29
为什么很多电器设备都要使用单片机?
2019-04-29
在中国做操作系统研发 20 年是种什么体验?
2019-04-29
PCB走线角度为90度到底行不行?
2019-04-29
这个 17 岁的黑客天才,破解了第一代 iPhone!
2019-04-29
在STM32价格疯长下,哪些国产32可以替代?
2019-04-29
半导体芯片原厂涨价及调价声明新增了这些!
2019-04-29
为什么你学C++这么难?
2019-04-29
无人机破巡检难题,秒变电网卫士
2019-04-29
五年,我成为了一名嵌入式工程师。
2019-04-29
2020年电赛题目,命题专家们怎么看?
2019-04-29
PCB元器件摆放不可忽略的10个技巧
2019-04-29