对我的代码执行输入验证的最有效方法是什么?

问题描述 投票:1回答:2

我正在尝试创建一个Android应用程序(最低API级别23,Android Studio 3.3.2,使用AndroidX Artefacts),用户可以在其中注册和登录;所有数据都将存储在Cloud Firestore中。

我对每个字段的输入验证要求如下:

  1. 名字:不能为空,不能包含空格,只包含字母+每个字符的自动大写
  2. 中间名:可以留空,可以包含空格,只包含字母+每个字符的自动大写
  3. 姓氏:不能为空,不能包含空格,只包含字母+每个字符的自动大写
  4. 部门:不能为空,可以包含空格,字母和特殊字符+每个字符的自动大写
  5. 程序:不能为空,可以包含空格,字母和特殊字符+每个字符的自动大写
  6. 学期:不能为空,只能包含0-9之间的数字,但该值不能小于1
  7. Rollnumber:不能为空,不能包含空格,并且是每个字符的字母数字+自动大写
  8. 电子邮件:不能为空+必须与标准电子邮件模式匹配
  9. 密码:不能为空,最少7个字符,1个大写字母,1个小写字母,1个数字,1个特殊符号作为最低限度
  10. Passconfirm:必须与上面的“密码”字段匹配

上面的所有输入字段都将存储为字符串,但学期将是一个整数

下面是我的注册过程代码,它是工作代码,因为数据已成功添加到数据库,但我还没有找到验证输入的方法。

注册类代码:

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>
java android google-cloud-firestore
2个回答
2
投票

试试这个[1]。 https://github.com/thyrlian/AwesomeValidation

它具有使用textwatchers和文本输入的验证器。

您还可以使用Textinputlayout和文本输入编辑文本在Edittext上有效地显示错误。


0
投票

使用textchangedlistener然后它会在输入的文本错误或杂项时显示错误。谢谢

edittext.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before,
        int count) {
        if(!s.equals("") ) { 
            //do your work here 
        }
}



public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {

}

public void afterTextChanged(Editable s) {

}
});
© www.soinside.com 2019 - 2024. All rights reserved.