I am trying to create an Android App (Minimum API Level 23, Android Studio 3.3.2, Using AndroidX Artefacts) where a user can register and login; All data is to be stored on Cloud Firestore.
My input validation requirements for each field are as follows:
Firstname: Cannot be empty, cannot contain whitespaces and contains only alphabets + automatic capitalisation of each character
Middlename: Can be left empty, can contain whitespaces and contains only alphabets + automatic capitalisation of each character
Lastname: Cannot be empty, cannot contain whitespaces and contains only alphabets + automatic capitalisation of each character
Department: Cannot be empty, can contain whitespaces, alphabets and special characters + automatic capitalisation of each character
Programme: Cannot be empty, can contain whitespaces, alphabets and special characters + automatic capitalisation of each character
Semester: Cannot be empty and can contain only numbers ranging from 0-9 but the value cannot be less than 1
Rollnumber: Cannot be empty, cannot contain whitespaces and is alphanumeric + automatic capitalisation of each character
Email: Cannot be empty + must match the standard email pattern
Password: Cannot be empty, minimum of 7 characters with 1 Upper case, 1 lower case, 1 digit, 1 special symbol as the bare minimum
Passconfirm: Must match the "Password" field above
All input fields above will be stored as a string except for Semester which will be an integer
Below is my code for the registration process, it is working code as the data is added successfully to the database however I've yet to find a way to validate my input.
Code from Registration Class:
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();
}
});
}
}
Code from Student Class:
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;
}
}
My activity_regiser.xml file
<?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>