java - 对我的代码执行输入验证的最有效方法是什么?
问题描述
我正在尝试创建一个用户可以注册和登录的 Android 应用程序(最低 API 级别 23,Android Studio 3.3.2,使用 AndroidX Artefacts);所有数据都将存储在 Cloud Firestore 上。
我对每个字段的输入验证要求如下:
名字:不能为空,不能包含空格并且只包含字母+每个字符的自动大写
中间名:可以留空,可以包含空格并且只包含字母+每个字符的自动大写
姓氏:不能为空,不能包含空格并且只包含字母+每个字符的自动大写
部门:不能为空,可以包含空格、字母和特殊字符+每个字符自动大写
程序:不能为空,可以包含空格、字母和特殊字符+每个字符自动大写
学期:不能为空,只能包含0-9范围内的数字,但值不能小于 1
Rollnumber:不能为空,不能包含空格,并且是字母数字+每个字符的自动大写
电子邮件:不能为空 + 必须与标准电子邮件模式匹配
密码:不能为空,最少 7 个字符,最少 1 个大写字母、1 个小写字母、1 个数字、1 个特殊符号
Passconfirm:必须与上面的“密码”字段匹配
上面的所有输入字段都将存储为一个字符串,除了 Semester 它将是一个整数
下面是我的注册过程代码,它是工作代码,因为数据已成功添加到数据库中,但是我还没有找到一种方法来验证我的输入。
注册类代码:
package com.university.smartattendance;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener
{
EditText TextFname;
EditText TextMname;
EditText TextLname;
EditText TextDept;
EditText TextProg;
EditText TextSemester;
EditText TextRolln;
EditText TextEmail;
EditText TextPassword;
EditText TextPasswordConfirm;
FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
TextFname= findViewById(R.id.Fname);
TextMname= findViewById(R.id.Mname);
TextLname= findViewById(R.id.Lname);
TextDept= findViewById(R.id.Dept);
TextProg= findViewById(R.id.Prog);
TextSemester= findViewById(R.id.Semester);
TextRolln= findViewById(R.id.Rolln);
TextEmail= findViewById(R.id.Email);
TextPassword= findViewById(R.id.Password);
TextPasswordConfirm= findViewById(R.id.PasswordConfirm);
findViewById(R.id.submitbutton).setOnClickListener(this);
}
@Override
public void onClick (View V)
{
String Firstname=TextFname.getText().toString().trim();
String Middlename=TextMname.getText().toString();//Removed trimming
String Lastname=TextLname.getText().toString().trim();
String Department=TextDept.getText().toString().trim();
String Programme=TextProg.getText().toString().trim();
String Semester=TextSemester.getText().toString().trim();
String Rollnumber=TextRolln.getText().toString().trim();
String Email=TextEmail.getText().toString().trim();
String Password=TextPassword.getText().toString().trim();
String Passconfirm=TextPasswordConfirm.getText().toString().trim();
CollectionReference dbUsers = db.collection("Students");
Student student = new Student
(
Firstname,
Middlename,
Lastname,
Department,
Programme,
Integer.parseInt(Semester),
Rollnumber,
Email,
Password,
Passconfirm
);
dbUsers.add(student)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>()
{
@Override
public void onSuccess(DocumentReference documentReference)
{
Toast.makeText(RegisterActivity.this,"Successfully Registered", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Toast.makeText(RegisterActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
学生班的代码:
package com.university.smartattendance;
public class Student
{
private String Firstname,Middlename,Lastname,Department,Programme;
private int Semester;
private String Rollnumber,Email,Password,Passconfirm;
public Student()
{
}
public Student(String firstname, String middlename, String lastname, String department, String programme, int semester, String rollnumber, String email, String password, String passconfirm)
{
Firstname = firstname;
Middlename = middlename;
Lastname = lastname;
Department = department;
Programme = programme;
Semester = semester;
Rollnumber = rollnumber;
Email = email;
Password = password;
Passconfirm = passconfirm;
}
public String getFirstname()
{
return Firstname;
}
public String getMiddlename()
{
return Middlename;
}
public String getLastname()
{
return Lastname;
}
public String getDepartment()
{
return Department;
}
public String getProgramme()
{
return Programme;
}
public int getSemester()
{
return Semester;
}
public String getRollnumber()
{
return Rollnumber;
}
public String getEmail()
{
return Email;
}
public String getPassword()
{
return Password;
}
public String getPassconfirm()
{
return Passconfirm;
}
}
我的 activity_regiser.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/registerform"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="insideInset"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="44dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="97dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="97dp"
android:autofillHints=""
android:ems="10"
android:hint="First Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/Mname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Middle Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Fname" />
<EditText
android:id="@+id/Lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Last Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Mname" />
<EditText
android:id="@+id/Rolln"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Roll Number"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Lname" />
<EditText
android:id="@+id/Dept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Department"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Rolln" />
<EditText
android:id="@+id/Prog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Programme"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Dept" />
<EditText
android:id="@+id/Semester"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Semester"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Prog" />
<EditText
android:id="@+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:autofillHints=""
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Semester" />
<EditText
android:id="@+id/Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="99dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Email"
android:autofillHints="" />
<EditText
android:id="@+id/PasswordConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="97dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="97dp"
android:autofillHints=""
android:ems="10"
android:hint="Re-enter Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Password" />
<Button
android:id="@+id/submitbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="159dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="160dp"
android:text="Submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/PasswordConfirm" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
解决方案
试试这个 [1]。 https://github.com/thyrlian/AwesomeValidation
它有使用文本观察器和文本输入的验证器。
您还可以使用 Textinputlayout 和文本输入编辑文本在 Edittext 上有效地显示错误。
推荐阅读
- c++ - 异步运行 SSLSocket
- javascript - 未捕获的类型错误:无法读取 HTMLAnchorElement 处未定义的属性“顶部”。
- django - Django LOGGING 不打印到控制台和文件
- reactjs - Rematch/Reducers - 编写测试用例时出错
- android - [Android] Plaid App 中的 LiveData 混淆
- python - Python3 打印CR和一些文本时,文本没有被擦除
- bash - linux将输出存储到变量的概念是什么?
- java - 从服务器通过 API 传输数据完成后,如何使用远程数据构建 recyclerview
- java - Quarkus Panache Hibernate:如何清除缓存,使实体世界匹配数据库状态?
- javascript - 首次使用后删除该功能