Advertisements

Regex & Validations in Android

Regular Expression or Regex is a very important expression matcher for applying validations in any android application. You can learn Regex. It is very easy to learn or you can find some combinations (or expressions) online for Email or password verification and much more. If you still find it difficult to create a Regex you can take some help from online tools or just mention your issue in Comment Box and I will solve it ASAP.

You can Watch my Video Tutorial or follow Steps Below:

What validations and Regex we are going to use: 

  1. The full name can contain Lowercase and Uppercase letters and also blank spaces.
  2. Email Address Validation through a regex.
  3. Mobile number should be at least 10 digits in length.
  4. Password validation through regex containing lowercase, uppercase, numbers, and special characters.

To design a simple sign up page create an activity and go to layout file activitymain.xml and paste the code below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context="com.infy.regexvalidation.MainActivity"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:textSize="25sp"
        android:textStyle="bold" />
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tipname"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edtname"
            android:hint="Enter Full Name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tipemail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edtemail"
            android:hint="Enter Email"
            android:inputType="textEmailAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tipmobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:maxLength="10"
            android:id="@+id/edtmobile"
            android:hint="Enter Mobile Number"
            android:inputType="number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tippassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edtpassword"
            android:hint="Enter Password"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/tipconfirmpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edtconfirmpassword"
            android:hint="Enter Password"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.TextInputLayout>
   <Button
       android:id="@+id/btnsignup"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Sign Up"
       android:textColor="#ffff"
       android:textSize="16sp"
       android:layout_marginTop="10sp"
       android:background="#16a085"/>
</LinearLayout>

 

 

Also, add these lines to app gradle file:

compile 'com.android.support:design:26.1.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

 

Now Create a Java Class Validation.java and paste the following code:

public class Validation {
    public boolean fullname(String name){

        String namepattern = "^[\\p{L} .'-]+$";
        if (name.matches(namepattern))
        {
         return true;
        }
        else
        {
            return false;
        }

    }
    public boolean email(String email){

        String emailpattern = "[a-zA-Z0-9._-]+@[a-z]+.[a-z]+";
        if (email.matches(emailpattern))
        {
         return true;
        }
        else
        {
            return false;
        }

    }
    public boolean password(String pass){

        String passpattern = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
        if (pass.matches(passpattern))
        {
         return true;
        }
        else
        {
            return false;
        }

    }
}

 

 

Now Go to MainActivity.java and write the following code:

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
@BindView(R.id.tipname)
    TextInputLayout tipname;
@BindView(R.id.edtname)
    EditText edtname;
@BindView(R.id.tipemail)
    TextInputLayout tipemail;
@BindView(R.id.edtemail)
EditText edtemail;
@BindView(R.id.tipmobile)
TextInputLayout tipmobile;
@BindView(R.id.edtmobile)
EditText edtmobile;
@BindView(R.id.tippassword)
TextInputLayout tippass;
@BindView(R.id.edtpassword)
EditText edtpass;
@BindView(R.id.tipconfirmpassword)
    TextInputLayout tipconfirmpass;
@BindView(R.id.edtconfirmpassword)
EditText edtconformpass;
@BindView(R.id.btnsignup)
    Button btnsignup;
Validation validation;
String name,email,mobile,pass,confirmpass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
       validation=new Validation();
        btnsignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name=edtname.getText().toString().trim();
                email=edtemail.getText().toString().trim();
                mobile=edtmobile.getText().toString().trim();
                pass=edtpass.getText().toString().trim();
                confirmpass=edtconformpass.getText().toString().trim();
                if (name.isEmpty()){
                    tipname.setError("Enter Your Name");
                }else
                if (!validation.fullname(name)){
                    tipname.setError("Only Small/Upper case Letters are allowed");
                }
                else if (email.isEmpty()){
                    tipname.setError(null);
                    tipemail.setError("Please Enter Email Address");
                } else if (!validation.email(email)){
                    tipname.setError(null);
                    tipemail.setError("Enter Correct Email Address");
                }else if (mobile.isEmpty()){
                    tipname.setError(null);
                    tipemail.setError(null);
                    tipmobile.setError("Enter Your Mobile Number");
                }else if (mobile.length()<10){
                    tipname.setError(null);
                    tipemail.setError(null);
                    tipmobile.setError("Enter Valid Mobile Number");
                }else if (pass.isEmpty()){
                    tipname.setError(null);
                    tipemail.setError(null);
                    tipmobile.setError(null);
                    tippass.setError("Enter Password");
                }else if (!validation.password(pass)){
                    tipname.setError(null);
                    tipemail.setError(null);
                    tipmobile.setError(null);
                    tippass.setError("Enter 8 Digit with upper & lower case letter and special charaters Password");
                } else if (!pass.equals(confirmpass)){
                    tipname.setError(null);
                    tipemail.setError(null);
                    tipmobile.setError(null);
                    tippass.setError(null);
                    tipconfirmpass.setError("Password Did not Match");
                }else{
                    tipconfirmpass.setError(null);
                    tippass.setError(null);
                    tipemail.setError(null);
                    tipname.setError(null);
                    tipmobile.setError(null);
                    Toast.makeText(MainActivity.this, "Sign Up Successfull", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

 

 

Done!

Notes:

  1. You learned how to perform validation via creating another class or for simple validation click here.
  2. You learned the easiest way to implement regex to match patterns to apply validations in android.
  3. Using TextInputLayout to display errors. Learn to Design a Login Page here or learn using font awesome in android here.

 

How useful was this post?

Do not forget to rate! this helps us to Improve.

Average rating / 5. Vote count:

Read More Like This..

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Advertisements

Safiya Akhtar

Safiya Akhtar is Android Application Developer in India. She is currently working as a public consultant for career and education too, besides blogging and freelancing. She believes that it is never too late or too early to start what you have always dreamed of.

Your Views And Question are Welcome Here!

Advertisements