DEV Community

Wajahat Karim 🇵🇰
Wajahat Karim 🇵🇰

Posted on • Updated on

Quickly & Easily Validating Your Text with Easy Validation

Originally posted on my Medium profile

In this data-driven era of technology, almost all websites and apps have one thing in common. That’s text validation for their data input forms and pages. This task of validating input and text before calling web services and APIs have become a routine for developers.

In Android, this sometimes become too complex and tedious to perform validations and checks on multiple EditText and TextInputLayout views. Specially, when it comes to perform multiple checks to make sure that your user’s password is unbreakable and complex. These kinds of tasks often becomes boring for android developers and can cause major security vulnerabilities if not managed properly.

I was also frustrated in this situation and after searching through Internet and GitHub and using various third-party libraries, I have come up with this easy validation library surprisingly called as EasyValidation

EasyValidation is an android library developed in Kotlin to provide an easy-to-use way to perform validation checks on any Text input through any String , EditText, TextView, AutoCompleteTextView, TextInputLayout, and Spinner using the power of Kotlin Extension methods and higher order functions. In future, more views will be added in this list.

How it works?

Now let’s see what EasyValidation can do for us. First, you have to include it in your android projects using Gradle like this:

Now for example, you can validate any email string like this:

Not only String or Text , you can perform these checks on EditText, TextView, AutoCompleteTextView, TextInputLayout, and Spinner like this:

There are around 30+ built-in rules in the core module library such as email, empty, number, credit card number etc. You can check all these in Rules page.

You can also apply multiple checks to make complex input very easily. For example, you want user’s password to have at least 8 characters length with one special character, one uppercase letter and one number. Then you can do these types of checks like this:

You can see how easy is to perform multiple validation checks. One thing to note here is the check() method. When you perform multiple validations, you will have to manually call check() method in order to start validations. In single validation, this method is called automatically.

You can not only perform multiple validations on same text, but you can also perform same validation on multiple text streams at same time by using Collection Extensions. For example, you are taking a guest invite list of email addresses, and you have to see that all the texts are valid email addresses. Then you can do it like this:

Along with 30+ built-in rules, you can create your own custom rules very easily.

First step is you have to create your Rule class by extending BaseRule or any other existing rule like this.

You can use this rule using Validator#addRule() method like this:

EasyValidation Library and Documentation

You can check the library and it’s code at GitHub at below link.
EasyValidation

GitHub logo wajahatkarim3 / EasyValidation

✔️ A text and input validation library in Kotlin for Android

New in the EasyValidation  The article on how this library was created is now published. You can read it on this link here. →


Built with ❤︎ by Wajahat Karim and contributors


✔️ Changelog

Changes exist in the releases tab.

💻 Installation

Add this in app's build.gradle file:

   implementation "com.wajahatkarim3.easyvalidation:easyvalidation-core:1.0.1"

📄 Documentation

Full documentation is available at Gitbook.

⭐️ Features

  • The Validator way validation support. - Details
  • 30+ built-in validation rules like empty, email, credit cards, etc. - Rules List
  • Extension methods for String, EditText, TextView, AutoCompleteTextView, TextInputLayout, and Spinner. - Details
  • Multiple Validations and Checks - Details
  • Collection Extension Methods for validations on multiple texts and views - Details
  • Create your own custom rules - Details

Quick Usage

For example, you can validate any email String like this:

   var myEmailStr = "john.doe@gmail.com"
   var isValid = myEmailStr.validEmail()  // isValid will be

And for the usage and documentation for how to use it, you can check it at GitBook at below link.
Documentation

...

Wajahat Karim is a graduate from NUST, Islamabad, an experienced mobile developer, an active open source contributor, and co-author of two books Learning Android Intents and Mastering Android Game Development with Unity. In his spare time, he likes to spend time with his family, do experiments on coding, loves to write about lots of things (mostly on blog and medium) and is passionate contributor to open source. In June 2018, one of his library became #1 on Github Trending. His libraries have about 2000 stars on Github and are being used in various apps by the developers all around the globe. Follow him on Twitter and Medium to get more updates about his work in Writing, Android and Open Source.

Top comments (1)

Collapse
 
rokkoo profile image
Alfonso

So helpful! Ty for share 😊