Registration Form Validation in Android Studio

by | Jan 26, 2020 | Android, Android for Beginners, Android UI/UX Tutorials, Basics, Bull's Rent, UI UX | 4 comments

Registration form in Android with validation

material design Signup screen

Firstly, make material design signup screen for user registration form with validation.We will validate full name, username, email, phoneno, password.Then we have to assign id’s to all the fields. If you guys don’t know how to make material design signup screen and assign id’s so you guys can check aur previoues tutorial, click here

Create Activity SignUp

After making SignUp material design screen, then we have to create “signup” activity.

Then in this “signup” activity .We have to create register user function on GO button because by clicking on GO button, all the data in the fields help the user to SignUp.

Make sure while creating “registeruser” function, it should be public and single parameter of view.

In this “registeruser” function, we have to get all values in string for registration form in android with validation and store it in firebase.

To get more detail and code, please see aur tutorial, click here

Validate registration form

Validate Name

Firstly, we have to validate name. So. for  this,  make function boolean and it should be named “validatename()”.

Inside this function, we have to get data from aur text field like this

 

String val = regName.getEditText().getText().toString();

Then, we have to check if the field is empty or not. If the field is empty, simply, call that field and set error. As we are using material design edit text field. So, we already have set error space inside the layout of edit text. We have to set error that ” field cannot be empty” otherwise return false if field is empty.

In else section, similarly set text field error and pass null, so this null remove error generated for first time. Again when user entered data, erroe will be gone and return true.

Code is given below

private Boolean validateName() {
    String val = regName.getEditText().getText().toString();

    if (val.isEmpty()) {
        regName.setError("Field cannot be empty");
        return false;
    } 
    else {
        regName.setError(null);
        regName.setErrorEnabled(false);
        return true;
     }
}

Validate Username

For the username, what we want to do is. field should not be empty. Then we have to add length limitation, if the length of field is greater than 15, seterror “username too long” and return false.

Add a variable string. It will check if there any space between username it generate error. For this simply, if the field not matches the requirment of no whidespace between username. Then generate error “white spaces not allowed” and return false.

Code of username given below.

private Boolean validateUsername() {
    String val = regUsername.getEditText().getText().toString();
    String noWhiteSpace = "\\A\\w{4,20}\\z";

    if (val.isEmpty()) {
        regUsername.setError("Field cannot be empty");
        return false;
     } else if (val.length() >= 15) {
         regUsername.setError("Username too long");
         return false;
     } else if (!val.matches(noWhiteSpace)) {
          regUsername.setError("White Spaces are not allowed");
          return false;
     } else {
         regUsername.setError(null);
         regUsername.setErrorEnabled(false);
         return true;
     }
}

Validate Email

In validate email, we have to make boolean function “validateemail()”, same as we done in validate username above. The things we have to add in validate email is, we have to make another string which is called rigix. Then we have to validate email, if value in text field not match with email pattern, it will show error”invalid email”and return false.

Code give below

private Boolean validateEmail() {
        String val = regEmail.getEditText().getText().toString();
        String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

        if (val.isEmpty()) {
            regEmail.setError("Field cannot be empty");
            return false;
        } else if (!val.matches(emailPattern)) {
            regEmail.setError("Invalid email address");
            return false;
        } else {
            regEmail.setError(null);
            regEmail.setErrorEnabled(false);
            return true;
        }
    }

Validate PhoneNo

Then, we have to validate “PhoneNO”. Check, either the field are empty or not. If the field is empty, simply, call that field and set error. As we are using material design edit text field. We have to set error that ” field cannot be empty” otherwise return false if field is empty.

In else section, similarly set text field error and pass null, so this null remove error generated for first time. Again when user entered data, error will be gone and return true.

Code given below.

private Boolean validatePhoneNo() {
        String val = regPhoneNo.getEditText().getText().toString();

        if (val.isEmpty()) {
            regPhoneNo.setError("Field cannot be empty");
            return false;
        } else {
            regPhoneNo.setError(null);
            regPhoneNo.setErrorEnabled(false);
            return true;
        }
    }

Validate Password

In this validate password. Firstly, we have to make boolean function “validatepassword”.  Then we have to make string for password. You guys can set password type as per your requirment as shown in code.

String passwordVal = "^" +
                //"(?=.*[0-9])" +         //at least 1 digit
                //"(?=.*[a-z])" +         //at least 1 lower case letter
                //"(?=.*[A-Z])" +         //at least 1 upper case letter
                "(?=.*[a-zA-Z])" +      //any letter
                "(?=.*[@#$%^&+=])" +    //at least 1 special character
                "(?=\\S+$)" +           //no white spaces
                ".{4,}" +               //at least 4 characters
                "$";

Then, inside validate password. We have to check, if the password doesnot match with password pattern. It will set error”Invalid password” and return false.

Code is given below.

private Boolean validatePassword() {
        String val = regPassword.getEditText().getText().toString();
        String passwordVal = "^" +
                //"(?=.*[0-9])" +         //at least 1 digit
                //"(?=.*[a-z])" +         //at least 1 lower case letter
                //"(?=.*[A-Z])" +         //at least 1 upper case letter
                "(?=.*[a-zA-Z])" +      //any letter
                "(?=.*[@#$%^&+=])" +    //at least 1 special character
                "(?=\\S+$)" +           //no white spaces
                ".{4,}" +               //at least 4 characters
                "$";

        if (val.isEmpty()) {
            regPassword.setError("Field cannot be empty");
            return false;
        } else if (!val.matches(passwordVal)) {
            regPassword.setError("Password is too weak");
            return false;
        } else {
            regPassword.setError(null);
            regPassword.setErrorEnabled(false);
            return true;
        }
    }

//This function will execute when user click on Register Button
    public void registerUser(View view) {

        if(!validateName() | !validatePassword() | !validatePhoneNo() | !validateEmail() | !validateUsername()){
            return;
        }
        
    }

After validating all function above. Then we have to call all the function one by one in register user function. If any of these function are not valid then it return to registeruser and show error, otherwise it will store and show data in firebase. thats how the registration in android with validation takes place.

//This function will execute when user click on Register Button 
public void registerUser(View view) { 
	if(!validateName() |!validatePassword() | !validatePhoneNo() | !validateEmail() | !validateUsername())
	{ 
		return; 
	} 
}

Get Complete Source code for Bull’s Rent App

CODING with T

🚀 Supercharge your Flutter skills! Subscribe to my YouTube channel now for mind-blowing coding insights, expert tips, and exclusive content. Don’t miss out!

COURSES

Android firebase app - android app 2022 - firebase android app - bulls rent app - coding with t app - android app
android complete login signup product by taimoor sikander free for any type of android app
Flutter E Commerce App Modern and latest
Flutter Login App UI Source Code - Flutter Complete App - Flutter App Design - Flutter App 2023 - Complete Flutter App 2023
Learn flutter from scratch in 4 hours - Flutter Crash Course - Coding with T

Latest Tutorial

How to create a Custom Appbar in flutter. Custom Appbar Design. Flutter App Design
How to create a Custom Shape in Flutter. Flutter E Commerce app Design. Ecommerce app design Flutter. Flutter clippath tutorial
Bottom Navigation bar in Flutter. Flutter Material 3 bottom navigation bar. How to design background color of Flutter bottom navigation bar. Flutter ecommerce app design
Access Admin Panel, Premium Tutorials, Live Chat, 50% off Codes, and More on Patreon!
This is default text for notification bar