Android Form Validation using Android-Validator library Tutorial

Udemy Generic 728x90

Objective

In this tutorial we learn how to validate the Form using Android-Validator Library in Android Application through simple example.

 

 

 

We define a rule to test if the provided string matches a specific format and then apply that rule to test the input string entered by user in an input box. If given string does not satisfy the validation rule then display a error message as notification. This whole process is known as Form Validation.

In this tutorial, we will use Android-Validator library to validate the input fields in the form. In this library, Validator is basic class that contains following specific validation rules:

  • EmailValidator : Checks whether input field contains an email address.
  • NotEmptyValidator : Ensures that the field is not empty.
  • HexValidator : Ensure that the field has Hex characters.
  • PhoneValidator : Checks whether the input string is a valid phone number.
  • RangeValidator : Checks whether input is between a range of values.
  • IPAddressValidator : Checks whether the input is valid IpAddress
  • NumericValidator : Checks whether the field contains only numeric digits
  • UrlValidator : Ensures that the input is a valid url.

We will use EditText widget to input the string and Button to perform validation on its click and when input string is incorrect, display an error message.

 

Step 2

Create New Project

Create a new Project in Android Studio, goto File ⇒ New ⇒ New Projects.

Step 3

Create Layout

We will create a simple form by using EditText and Button. So, add the EditText widget to get the input string and Button to submit the form. Use the following code in the activity_main.xml file.

<br />
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br />
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br />
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br />
    android:layout_width=&quot;match_parent&quot;<br />
    android:layout_height=&quot;match_parent&quot;<br />
    android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;<br />
    android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;<br />
    android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;<br />
    android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;<br />
    tools:context=&quot;app.formvalidation3.MainActivity&quot;&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Non Empty&quot;<br />
        android:id=&quot;@+id/nonempty&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Enter Url&quot;<br />
        android:layout_below=&quot;@+id/nonempty&quot;<br />
        android:id=&quot;@+id/url&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Enter Email&quot;<br />
        android:layout_below=&quot;@+id/url&quot;<br />
        android:id=&quot;@+id/email&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Enter Phone&quot;<br />
        android:layout_below=&quot;@+id/email&quot;<br />
        android:id=&quot;@+id/phone&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Enter ipAddress&quot;<br />
        android:layout_below=&quot;@+id/phone&quot;<br />
        android:id=&quot;@+id/ipaddress&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Enter Only Number&quot;<br />
        android:layout_below=&quot;@+id/ipaddress&quot;<br />
        android:id=&quot;@+id/numeric&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Range&quot;<br />
        android:layout_below=&quot;@+id/numeric&quot;<br />
        android:id=&quot;@+id/range&quot;/&gt;</p>
<p>    &lt;EditText<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:hint=&quot;Enter Hexa Character&quot;<br />
        android:layout_below=&quot;@+id/range&quot;<br />
        android:id=&quot;@+id/hexa&quot;/&gt;</p>
<p>    &lt;Button<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:text=&quot;submit&quot;<br />
        android:onClick=&quot;validate_form&quot;<br />
        android:layout_below=&quot;@+id/hexa&quot;<br />
        android:id=&quot;@+id/submit&quot;/&gt;</p>
<p>&lt;/RelativeLayout&gt;</p>
<p>

form
 

Step 4

Download library

Download the Android Validtor library. Unzip the downloaded file into project directory.

 

Step 5

Include library

To compile Android Validtor library in the project, we need to add it into build.gradle(app module) file. So, open your build.gradle file and add this library as the dependency.

<br />
dependencies {<br />
    ....<br />
    compile project (':library')<br />
    ...<br />
}<br />

 

Step 6

Initialize the EditText

We need to initialize EditText to get input string. So, create a initializeEditText() method and within this method, initialize all of the custom EditText widgets to get input string when needed.

<br />
public class MainActivity extends AppCompatActivity {<br />
    EditText non_empty, email, url, phone, ipAddress, numeric, range, hexa;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
        setContentView(R.layout.activity_main);</p>
<p>        // Initialize the EditText by calling this method<br />
        initializeEditText();<br />
    }<br />
    /**<br />
     * This method is used to initialize all the EditText.<br />
     */<br />
    public void initializeEditText(){</p>
<p>        non_empty = (EditText) findViewById(R.id.nonempty);<br />
        email = (EditText) findViewById(R.id.email);<br />
        url = (EditText) findViewById(R.id.url);<br />
        phone = (EditText) findViewById(R.id.phone);<br />
        ipAddress = (EditText) findViewById(R.id.ipaddress);<br />
        numeric = (EditText) findViewById(R.id.numeric);<br />
        range = (EditText) findViewById(R.id.range);<br />
        hexa = (EditText) findViewById(R.id.hexa);<br />
    }<br />
}<br />

 

Step 7

Add Validation Rules

Next, we need to add the validation rule on every EditText widget. To do so, first we have to get an instance of Validate class for every field by passing the field instance to it. Then, call addValidator() method of Validate class to add the validation rule. To define the type of validation rule, we need to pass instance of specific validation class as param to addValidator() method.  Here is the code snippet for same :

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    EditText non_empty, email, url, phone, ipAddress, numeric, range, hexa;<br />
    Validate nonempty_validate, email_validate, url_validate, phone_validate,<br />
             ipAddress_validate, numeric_validate,<br />
             range_validate, hexa_validate;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
        setContentView(R.layout.activity_main);</p>
<p>        // Initialize the EditText by calling this method<br />
        initializeEditText();<br />
    }</p>
<p>   /**<br />
    * This method is used to initialize all the EditText fields.<br />
    */<br />
    public void initializeEditText(){</p>
<p>        non_empty = (EditText) findViewById(R.id.nonempty);<br />
        email = (EditText) findViewById(R.id.email);<br />
        url = (EditText) findViewById(R.id.url);<br />
        phone = (EditText) findViewById(R.id.phone);<br />
        ipAddress = (EditText) findViewById(R.id.ipaddress);<br />
        numeric = (EditText) findViewById(R.id.numeric);<br />
        range = (EditText) findViewById(R.id.range);<br />
        hexa = (EditText) findViewById(R.id.hexa);<br />
    }</p>
<p>   /*<br />
    * Adding validation rules on the editText separately<br />
    */<br />
    public void addValidationRules(){</p>
<p>        nonempty_validate = new Validate(non_empty);<br />
        nonempty_validate.addValidator(new NotEmptyValidator(this));</p>
<p>        email_validate = new Validate(email);<br />
        email_validate.addValidator(new EmailValidator(this));</p>
<p>        url_validate = new Validate(url);<br />
        url_validate.addValidator(new UrlValidator(this));</p>
<p>        phone_validate = new Validate(phone);<br />
        phone_validate.addValidator(new PhoneValidator(this));</p>
<p>        ipAddress_validate = new Validate(ipAddress);<br />
        ipAddress_validate.addValidator(new IPAddressValidator(this));</p>
<p>        numeric_validate = new Validate(numeric);<br />
        numeric_validate.addValidator(new NumericValidator(this));</p>
<p>        range_validate = new Validate(range);<br />
        range_validate.addValidator(new RangeValidator(this,0.0,120.0, R.string.range));</p>
<p>        hexa_validate = new Validate(hexa);<br />
        hexa_validate.addValidator(new HexValidator(this));<br />
    }<br />
}<br />

 

Step 8

Validate the form

Now, we will validate the form by using validate() method. So, create a callback validate_form() method. This method will be called when user click on the submit button. In this method, we create a Form object and pass Validate object to form object by using addValidates() method.

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    EditText non_empty, email, url, phone, ipAddress, numeric, range, hexa;<br />
    Validate nonempty_validate, email_validate, url_validate, phone_validate,<br />
             ipAddress_validate, numeric_validate,<br />
             range_validate, hexa_validate;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
        setContentView(R.layout.activity_main);</p>
<p>        // Initialize the EditText by calling this method<br />
        initializeEditText();<br />
    }</p>
<p>   /**<br />
    * This method is used to initialize all the EditText fields.<br />
    */<br />
     public void initializeEditText(){<br />
        non_empty = (EditText) findViewById(R.id.nonempty);<br />
        email = (EditText) findViewById(R.id.email);<br />
        url = (EditText) findViewById(R.id.url);<br />
        phone = (EditText) findViewById(R.id.phone);<br />
        ipAddress = (EditText) findViewById(R.id.ipaddress);<br />
        numeric = (EditText) findViewById(R.id.numeric);<br />
        range = (EditText) findViewById(R.id.range);<br />
        hexa = (EditText) findViewById(R.id.hexa);<br />
    }</p>
<p>   /*<br />
    * Adding validation rules on the editText separately<br />
    */<br />
    public void addValidationRules(){</p>
<p>        nonempty_validate = new Validate(non_empty);<br />
        nonempty_validate.addValidator(new NotEmptyValidator(this));</p>
<p>        email_validate = new Validate(email);<br />
        email_validate.addValidator(new EmailValidator(this));</p>
<p>        url_validate = new Validate(url);<br />
        url_validate.addValidator(new UrlValidator(this));</p>
<p>        phone_validate = new Validate(phone);<br />
        phone_validate.addValidator(new PhoneValidator(this));</p>
<p>        ipAddress_validate = new Validate(ipAddress);<br />
        ipAddress_validate.addValidator(new IPAddressValidator(this));</p>
<p>        numeric_validate = new Validate(numeric);<br />
        numeric_validate.addValidator(new NumericValidator(this));</p>
<p>        range_validate = new Validate(range);<br />
        range_validate.addValidator(new RangeValidator(this,0.0,120.0, R.string.range));</p>
<p>        hexa_validate = new Validate(hexa);<br />
        hexa_validate.addValidator(new HexValidator(this));<br />
    }</p>
<p>    // apply validation rule on the form<br />
    public void validate_form(View view){</p>
<p>        Form mForm = new Form();<br />
        mForm.addValidates(nonempty_validate);<br />
        mForm.addValidates(email_validate);<br />
        mForm.addValidates(url_validate);<br />
        mForm.addValidates(phone_validate);<br />
        mForm.addValidates(ipAddress_validate);<br />
        mForm.addValidates(numeric_validate);<br />
        mForm.addValidates(range_validate);<br />
        mForm.addValidates(hexa_validate);</p>
<p>        // Launch Validation<br />
        if(mForm.validate()){</p>
<p>            Toast.makeText(getApplicationContext(),&quot;success&quot;,Toast.LENGTH_LONG).show();</p>
<p>        }else{<br />
           // Toast.makeText(getApplicationContext(),&quot;error&quot;,Toast.LENGTH_LONG).show();<br />
        }<br />
    }<br />
}<br />

 

requried validation
 

url validation
 

email validation
 

ipAddess validation
 

Range Validation
 

formValidation
 

Udemy Generic 728x90

Spread the word. Share this post!

  • Vishwas Soni

    how i handle exception when app crash after not validate value