Android Form validation using Android Form EditText Library Tutorial

Udemy Generic 728x90

Objective

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

 

 

In Form validation we apply validation rules on the input box. We validate the input data with custom rules and if data does not satisfy any of the validation rule then we display an error message.

In this tutorial, we will use Android Form EditText Library to validate the input field in the form.  We will apply following type of validation rules in our form:

  • personFullName: checks if the entered value is a complete full name.
  • email: checks that input data is a valid email.
  • phone: checks that the field contains a valid phone number.
  • creditCard: checks that the field contains a valid credit card using Luhn Algorithm.
  • date: Set the custom Date format. It checks field that contains valid date format.
  • domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 ).
  • ipAddress: checks that the field contains a valid ip address.
  • numeric: checks that filed contains only numeric value.
  • nocheck: It does not check anything except the emptyness of the field
  • webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
  • regexp: check for the custom expression/rule

We use custom EditText to input the string and button to perform validation on click and if input string is incorrect, then 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 custom EditText and Button widgets. So, add the custom 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<br />
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br />
    xmlns:editTextFormExample=&quot;http://schemas.android.com/apk/res-auto&quot;<br />
    xmlns:tools=&quot;http://schemas.android.com/tools&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.formvalidation2.MainActivity&quot;&gt;</p>
<p>    &lt;ScrollView<br />
        android:id=&quot;@+id/scroll&quot;<br />
        android:layout_marginBottom=&quot;40dp&quot;<br />
        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;&gt;</p>
<p>        &lt;LinearLayout<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:orientation=&quot;vertical&quot; &gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:id=&quot;@+id/full_name&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Full name&quot;<br />
            editTextFormExample:testType=&quot;personFullName&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/full_name&quot;<br />
            android:id=&quot;@+id/email&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Email&quot;<br />
            android:inputType=&quot;textEmailAddress&quot;<br />
            editTextFormExample:testType=&quot;nocheck&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/email&quot;<br />
            android:id=&quot;@+id/phone&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Phone&quot;<br />
            android:inputType=&quot;phone&quot;<br />
            editTextFormExample:testType=&quot;phone&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/phone&quot;<br />
            android:id=&quot;@+id/creditcart&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Credit card&quot;<br />
            android:inputType=&quot;number&quot;<br />
            editTextFormExample:testType=&quot;creditCard&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/creditcart&quot;<br />
            android:id=&quot;@+id/custom_date_format&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Custom Date Format&quot;<br />
            editTextFormExample:testType=&quot;date&quot;<br />
            editTextFormExample:customFormat=&quot;yyyy-MM-dd&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/custom_date_format&quot;<br />
            android:id=&quot;@+id/domain&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Domain&quot;<br />
            editTextFormExample:testType=&quot;domainName&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/domain&quot;<br />
            android:id=&quot;@+id/email_or_creditcard&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Email or Credit card&quot;<br />
            android:inputType=&quot;textEmailAddress&quot;<br />
            editTextFormExample:testType=&quot;nocheck&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/email_or_creditcard&quot;<br />
            android:id=&quot;@+id/ip_address&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Ip Address&quot;<br />
            editTextFormExample:testType=&quot;ipAddress&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/ip_address&quot;<br />
            android:id=&quot;@+id/numeric&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Integer only&quot;<br />
            editTextFormExample:testType=&quot;numeric&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/numeric&quot;<br />
            android:id=&quot;@+id/no_check&quot;<br />
            style=&quot;@android:style/Widget.EditText&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;No check&quot;<br />
            editTextFormExample:testType=&quot;nocheck&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/no_check&quot;<br />
            android:id=&quot;@+id/web_mail&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Web Url&quot;<br />
            android:inputType=&quot;textEmailAddress&quot;<br />
            editTextFormExample:testType=&quot;webUrl&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/web_mail&quot;<br />
            android:id=&quot;@+id/regexp&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            editTextFormExample:customRegexp=&quot;[A-Za-z]+&quot;<br />
            android:hint=&quot;Only alphabet&quot;<br />
            editTextFormExample:testErrorString=&quot;Only alphabet&quot;<br />
            editTextFormExample:testType=&quot;regexp&quot; /&gt;</p>
<p>        &lt;com.andreabaccega.widget.FormEditText<br />
            android:layout_below=&quot;@+id/regexp&quot;<br />
            android:id=&quot;@+id/empty&quot;<br />
            android:layout_width=&quot;match_parent&quot;<br />
            android:layout_height=&quot;wrap_content&quot;<br />
            android:hint=&quot;Empty allow&quot;<br />
            android:inputType=&quot;phone&quot;<br />
            editTextFormExample:emptyAllowed=&quot;true&quot;<br />
            editTextFormExample:testType=&quot;phone&quot; /&gt;</p>
<p>        &lt;/LinearLayout&gt;<br />
    &lt;/ScrollView&gt;</p>
<p>    &lt;Button</p>
<p>        android:layout_width=&quot;match_parent&quot;<br />
        android:layout_height=&quot;wrap_content&quot;<br />
        android:layout_alignParentBottom=&quot;true&quot;<br />
        android:text=&quot;Click&quot;<br />
        android:onClick=&quot;onClickValidate&quot;/&gt;</p>
<p>&lt;/RelativeLayout&gt;</p>
<p>

validation form

 

Step 4

Adding library

We need to add the Android Form EditText Library in the project to validate the form. Open build.gradle(app module) file and add the library as the dependencies.

<br />
dependencies {<br />
    ....<br />
    compile 'com.andreabaccega:android-form-edittext:[email protected]'<br />
    ...<br />
}<br />

 

Step 5

Initialize the EditText

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

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    FormEditText full_name, email, phone, creditcard, custom_date_format,<br />
                 domain, email_or_creditcard, ip_address, numeric, no_check, weburl,<br />
                 regexp;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
        setContentView(R.layout.activity_main);</p>
<p>        // Initialize the custom EditText by calling this method<br />
        initializeId();</p>
<p>    }</p>
<p>    /**<br />
     *  In this method, initialize the custom EditText<br />
     */<br />
    private void initializeId(){</p>
<p>        full_name = (FormEditText) findViewById(R.id.full_name);<br />
        email = (FormEditText) findViewById(R.id.email);<br />
        phone = (FormEditText) findViewById(R.id.phone);<br />
        creditcard = (FormEditText) findViewById(R.id.creditcart);<br />
        custom_date_format = (FormEditText) findViewById(R.id.custom_date_format);<br />
        domain = (FormEditText) findViewById(R.id.domain);<br />
        email_or_creditcard = (FormEditText) findViewById(R.id.email_or_creditcard);<br />
        ip_address = (FormEditText) findViewById(R.id.ip_address);<br />
        numeric = (FormEditText) findViewById(R.id.numeric);<br />
        no_check = (FormEditText) findViewById(R.id.no_check);<br />
        weburl = (FormEditText) findViewById(R.id.web_mail);<br />
        regexp = (FormEditText) findViewById(R.id.regexp);<br />
    }<br />
}<br />

 

Step 6

Add the validation rule

We need to add the validation rule on every custom EditText widget separately through addValidator() method and pass a OrValidator() object into addValidator() method. We will use OrValidator object to pass the error message string and validator class to set the the validation rule on the input box.

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    FormEditText full_name, email, phone, creditcard, custom_date_format,<br />
                 domain, email_or_creditcard, ip_address, numeric, no_check, weburl,<br />
                 regexp;</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 />
        initializeId();</p>
<p>        // Set the Validation rules on the EditText by calling this method<br />
        EditTextValidator();</p>
<p>    }</p>
<p>   /**<br />
    *  In this method, initialize the EditText<br />
    */<br />
    private void initializeId(){</p>
<p>        full_name = (FormEditText) findViewById(R.id.full_name);<br />
        email = (FormEditText) findViewById(R.id.email);<br />
        phone = (FormEditText) findViewById(R.id.phone);<br />
        creditcard = (FormEditText) findViewById(R.id.creditcart);<br />
        custom_date_format = (FormEditText) findViewById(R.id.custom_date_format);<br />
        domain = (FormEditText) findViewById(R.id.domain);<br />
        email_or_creditcard = (FormEditText) findViewById(R.id.email_or_creditcard);<br />
        ip_address = (FormEditText) findViewById(R.id.ip_address);<br />
        numeric = (FormEditText) findViewById(R.id.numeric);<br />
        no_check = (FormEditText) findViewById(R.id.no_check);<br />
        weburl = (FormEditText) findViewById(R.id.web_mail);<br />
        regexp = (FormEditText) findViewById(R.id.regexp);<br />
    }</p>
<p>    // set the Validation rule on the Edittext<br />
    public void EditTextValidator(){</p>
<p>        // set the validation on the input filed for name of person<br />
        full_name.addValidator(<br />
                new OrValidator(<br />
                        &quot;Please fill full name&quot;,</p>
<p>                        /**<br />
                         * we specify null as the message string cause the<br />
                         * Or validator will use his own message<br />
                         */<br />
                        new PersonFullNameValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for email<br />
        email.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not a valid email&quot;,<br />
                        new EmailValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for phone number<br />
        phone.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not a valid phone number&quot;,<br />
                        new PhoneValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for credit card number<br />
        creditcard.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not a valid credit card number&quot;,<br />
                        new CreditCardValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for date<br />
        custom_date_format.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid date format&quot;,<br />
                        new DateValidator(null,&quot;yyyy-MM-dd&quot;)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for the domain name<br />
        domain.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid Domain&quot;,<br />
                        new DomainValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed, either email or credit card<br />
        email_or_creditcard.addValidator(<br />
                new OrValidator(<br />
                        &quot;TThis is neither a creditcard or an email&quot;,<br />
                        new CreditCardValidator(null),<br />
                        new EmailValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for the ipAddress<br />
        ip_address.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid Ip Address&quot;,<br />
                        new IpAddressValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed to input only integer value<br />
        numeric.addValidator(<br />
                new OrValidator(<br />
                        &quot;Only Integer value&quot;,<br />
                        new NumericValidator(null)<br />
                )<br />
        );</p>
<p>        // this is not check any validation rule except phone validation<br />
        no_check.addValidator(<br />
                new OrValidator(<br />
                        &quot;No check&quot;,<br />
                        new NotValidator(null,new PhoneValidator(null))<br />
                )<br />
        );</p>
<p>        // set the validation rule for the web url<br />
        weburl.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid web url&quot;,<br />
                        new WebUrlValidator(null)<br />
                )<br />
        );</p>
<p>        //set the custom validation rule on the input box<br />
        regexp.addValidator(<br />
                new OrValidator(<br />
                        &quot;set the validation rule on the input filed for the Alphabet&quot;,<br />
                        new RegexpValidator(null, &quot;[A-Za-z]+&quot;)<br />
                )<br />
        );<br />
    }<br />
}<br />

 

Step 7

Validate Fields

We will validate the input filed by using testValidity() method. Also, we will create a onClickValidate() method. In this method, use testValidity() method to validate all the fields.

<br />
public class MainActivity extends AppCompatActivity {</p>
<p>    FormEditText full_name, email, phone, creditcard, custom_date_format,<br />
                 domain, email_or_creditcard, ip_address, numeric, no_check, weburl,<br />
                 regexp;</p>
<p>    @Override<br />
    protected void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
        setContentView(R.layout.activity_main);</p>
<p>         // Initialize the custom EditText by calling this method<br />
        initializeId();</p>
<p>        // Set the Validation rules on the custom EditText by calling this method<br />
        EditTextValidator();</p>
<p>    }</p>
<p>   /**<br />
    *  In this method, initialize the custom EditText<br />
    */<br />
    private void initializeId(){</p>
<p>        full_name = (FormEditText) findViewById(R.id.full_name);<br />
        email = (FormEditText) findViewById(R.id.email);<br />
        phone = (FormEditText) findViewById(R.id.phone);<br />
        creditcard = (FormEditText) findViewById(R.id.creditcart);<br />
        custom_date_format = (FormEditText) findViewById(R.id.custom_date_format);<br />
        domain = (FormEditText) findViewById(R.id.domain);<br />
        email_or_creditcard = (FormEditText) findViewById(R.id.email_or_creditcard);<br />
        ip_address = (FormEditText) findViewById(R.id.ip_address);<br />
        numeric = (FormEditText) findViewById(R.id.numeric);<br />
        no_check = (FormEditText) findViewById(R.id.no_check);<br />
        weburl = (FormEditText) findViewById(R.id.web_mail);<br />
        regexp = (FormEditText) findViewById(R.id.regexp);<br />
    }</p>
<p>    // set the Validation rule on the custom Edittext<br />
    public void EditTextValidator(){</p>
<p>        // set the validation on the input filed for name of person<br />
        full_name.addValidator(<br />
                new OrValidator(<br />
                        &quot;Please fill full name&quot;,</p>
<p>                        /**<br />
                         * we specify null as the message string cause the<br />
                         * Or validator will use his own message<br />
                         */<br />
                        new PersonFullNameValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for email<br />
        email.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not a valid email&quot;,<br />
                        new EmailValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for phone number<br />
        phone.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not a valid phone number&quot;,<br />
                        new PhoneValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for credit card number<br />
        creditcard.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not a valid credit card number&quot;,<br />
                        new CreditCardValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for date<br />
        custom_date_format.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid date format&quot;,<br />
                        new DateValidator(null,&quot;yyyy-MM-dd&quot;)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for the domain name<br />
        domain.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid Domain&quot;,<br />
                        new DomainValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed, either email or credit card<br />
        email_or_creditcard.addValidator(<br />
                new OrValidator(<br />
                        &quot;TThis is neither a creditcard or an email&quot;,<br />
                        new CreditCardValidator(null),<br />
                        new EmailValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed for the ipAddress<br />
        ip_address.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid Ip Address&quot;,<br />
                        new IpAddressValidator(null)<br />
                )<br />
        );</p>
<p>        // set the validation rule on the input filed to input only integer value<br />
        numeric.addValidator(<br />
                new OrValidator(<br />
                        &quot;Only Integer value&quot;,<br />
                        new NumericValidator(null)<br />
                )<br />
        );</p>
<p>        // this is not check any validation rule except phone validation<br />
        no_check.addValidator(<br />
                new OrValidator(<br />
                        &quot;No check&quot;,<br />
                        new NotValidator(null,new PhoneValidator(null))<br />
                )<br />
        );</p>
<p>        // set the validation rule for the web url<br />
        weburl.addValidator(<br />
                new OrValidator(<br />
                        &quot;This is not valid web url&quot;,<br />
                        new WebUrlValidator(null)<br />
                )<br />
        );</p>
<p>        //set the custom validation rule on the input box<br />
        regexp.addValidator(<br />
                new OrValidator(<br />
                        &quot;set the validation rule on the input filed for the ipAddress&quot;,<br />
                        new RegexpValidator(null, &quot;[A-Za-z]+&quot;)<br />
                )<br />
        );<br />
    }</p>
<p>    public void onClickValidate(View v) {</p>
<p>        // set all field in the array list<br />
        FormEditText[] allFields = { full_name, email, phone, creditcard, custom_date_format,<br />
                domain, email_or_creditcard, ip_address, numeric, no_check, weburl, regexp};</p>
<p>        boolean allValid = true;<br />
        for (FormEditText field: allFields) {</p>
<p>            // validate the input filed through testValidity method<br />
            allValid = field.testValidity() &amp;&amp; allValid;<br />
        }</p>
<p>        if (allValid) {</p>
<p>            Toast.makeText(this, &quot;valid&quot;, Toast.LENGTH_LONG).show();</p>
<p>        } else {<br />
            // EditText are going to appear with an exclamation mark and an explicative message.<br />
        }<br />
    }<br />
}<br />

 

applied validation
 

validation for name
 

validation for email
 


validation for credit card
 

Udemy Generic 728x90

Spread the word. Share this post!