1

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:

  1. Firstname: Cannot be empty, cannot contain whitespaces and contains only alphabets + automatic capitalisation of each character

  2. Middlename: Can be left empty, can contain whitespaces and contains only alphabets + automatic capitalisation of each character

  3. Lastname: Cannot be empty, cannot contain whitespaces and contains only alphabets + automatic capitalisation of each character

  4. Department: Cannot be empty, can contain whitespaces, alphabets and special characters + automatic capitalisation of each character

  5. Programme: Cannot be empty, can contain whitespaces, alphabets and special characters + automatic capitalisation of each character

  6. Semester: Cannot be empty and can contain only numbers ranging from 0-9 but the value cannot be less than 1

  7. Rollnumber: Cannot be empty, cannot contain whitespaces and is alphanumeric + automatic capitalisation of each character

  8. Email: Cannot be empty + must match the standard email pattern

  9. Password: Cannot be empty, minimum of 7 characters with 1 Upper case, 1 lower case, 1 digit, 1 special symbol as the bare minimum

  10. 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>
  • If available in Android: https://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-usingvalidator.html – Tom Mar 05 '19 at 12:56
  • @DarKNate, on submit you can get the string values from edit-text and later you can perform whatever validations you want, follow these links https://www.codebrainer.com/blog/registration-form-in-android-check-email-is-valid-is-empty and https://stackoverflow.com/questions/24729641/adding-validations-to-registration-form-in-android – Vijaya Varma Lanke Mar 05 '19 at 13:00
  • @Varma Would it be possible to perform validation as the user is typing the text instead of on-submit? Where an error message would be displayed just below the input field? If so, could you share some sample code, please? –  Mar 05 '19 at 13:03
  • 1
    yes, we can use text changed listener wait will post the code – Vijaya Varma Lanke Mar 05 '19 at 13:08
  • 1
    @DarkNate You need to use a TextWatcher for that – Yashovardhan99 Mar 05 '19 at 13:09
  • As a note, all of your "name" rules are broken. Spaces, hyphens, and apostrophes are all reasonable characters. (You're also not using Java code standards for your variable names.) – chrylis -cautiouslyoptimistic- Mar 05 '19 at 13:30
  • @chrylis What do you mean they are broken? For example, the first name "John" cannot contain spaces or special characters, apostrophes are meant to indicate possession and omissions if I understand correctly? Can you elaborate please regarding the variable names? I seem to be following the basic rules? https://mathbits.com/MathBits/Java/DataBasics/Namingrules.htm –  Mar 05 '19 at 13:37
  • [The standard.](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) In no particular order, "Jean-Marie", "O'Connor", "Vaughan Williams". – chrylis -cautiouslyoptimistic- Mar 05 '19 at 15:54
  • (And variable names should start with a lowercase character and use camel-case: `firstName`. You can see by the highlighting above that using non-standard naming causes the highlighter to misinterpret what kind of token `Firstname` is.) – chrylis -cautiouslyoptimistic- Mar 05 '19 at 15:55
  • @chrylis Thanks for the valuable information. –  Mar 05 '19 at 16:09

2 Answers2

3

Try this [1]. https://github.com/thyrlian/AwesomeValidation

It has validators using textwatchers and text inputs.

And you can also use Textinputlayout and text input edit text to show errors efficiently on Edittext.

raj kavadia
  • 926
  • 1
  • 10
  • 30
0

use textchangedlistener then it will show an error when the entered text is wrong or miscellaneous.Thank you

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

}
});
Vijaya Varma Lanke
  • 603
  • 2
  • 7
  • 19
  • I'd strongly advise using @raj javadia's method, as this one will end up cluttering your class with probably thousands of lines of code, mostly redundant. Validators are what you need in this case, instead of manual validation – Arthur Attout Mar 05 '19 at 13:19
  • @ArthurAttout How do I go about implementing these validators? –  Mar 05 '19 at 13:49
  • The link @raj posted has a comprehensive and complete README you can go through. – Arthur Attout Mar 05 '19 at 13:52