Android Studio 实现注册信息表单验证的源代码(实现账号,密码,邮箱,手机号验证)(1)

android:layout_weight=“1”

android:ems=“10”

android:gravity=“center”

android:inputType=“textPassword”

android:hint=“请输入密码”

/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:gravity=“center”

android:text=“邮箱:” />

<EditText

android:id=“@+id/email”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:gravity=“center”

android:inputType=“textEmailAddress”

android:hint=“请输入邮箱”

/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:gravity=“center”

android:text=“手机号:” />

<EditText

android:id=“@+id/phone”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:gravity=“center”

android:inputType=“p
hone”

android:maxLength=“11”

android:hint=“请输入手机号”

/>

<Button

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:textSize=“25sp”

android:onClick=“register”

android:text=“注册” />

<Button

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:textSize=“25sp”

android:onClick=“reset”

android:text=“重置” />

</androidx.constraintlayout.widget.ConstraintLayout>

二、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){

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现登陆注册功能需要以下步骤: 1. 创建一个新的Android Studio项目。 2. 在该项目中创建两个活动(Activity),一个用于登录页面,一个用于注册页面。 3. 在登录页面中,添加输入手机号的文本框和密码文本框,以及登录按钮。 4. 在注册页面中,添加输入手机号的文本框、密码文本框、确认密码文本框和注册按钮。 5. 在注册页面中,添加手机号验证功能。验证方法可以使用正则表达式,以确保输入的手机号符合格式要求。 6. 在注册页面中,添加密码验证功能。验证方法可以使用正则表达式,以确保输入的密码符合格式要求。 7. 在注册页面中,添加确认密码验证功能,以确保确认密码密码一致。 8. 在注册页面中,添加注册按钮的点击事件。当用户点击该按钮时,将用户输入的信息上传到服务器进行注册。 9. 在登录页面中,添加登录按钮的点击事件。当用户点击该按钮时,将用户输入的手机号密码上传到服务器进行验证,并且跳转到主页面。 下面是一个简单的手机号验证的代码示例: ```java // 在注册页面中添加以下代码: EditText phoneEditText = findViewById(R.id.phone_edit_text); Button registerButton = findViewById(R.id.register_button); registerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String phone = phoneEditText.getText().toString(); if (!isValidPhone(phone)) { Toast.makeText(RegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); return; } // 如果手机号格式正确,继续执行注册操作 } }); private boolean isValidPhone(String phone) { String regex = "^1[3-9]\\d{9}$"; return Pattern.matches(regex, phone); } ``` 这段代码会检查用户输入的手机号是否符合要求,如果不符合,则会弹出提示信息。如果符合要求,则可以继续执行注册操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值