Input validation can be painful. Building forms in any technology is a boring and tedious job, yet it’s one of the most common tasks in app development.
When implementing form validation, we typically face three critical concerns:
- In-depth data validation - Ensuring the data format is correct
- Mandatory fields checking - Making sure required fields aren’t empty
- Error presentation - Showing users what went wrong
Introducing FormEditText
To solve these pain points, I created an open-source library called FormEditText, available on GitHub.
The library enables developers to validate EditText fields through simple XML attributes, eliminating boilerplate validation code.
Basic Usage
First, define a custom namespace in your layout and add validation attributes:
<com.andreabaccega.widget.FormEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
app:testType="email"
app:emptyAllowed="false" />
Validation Behavior
When you call validate() on a FormEditText:
- Valid input: Returns
truewithout additional action - Invalid input: Returns
falseand automatically displays an error message
Built-in Validators
The library includes validators for:
- Email addresses
- Phone numbers
- Credit cards
- IP addresses
- Web URLs
- Custom regex patterns
- And more…
Try It Out
- Source code on GitHub
- Example application available on Google Play
The library has grown to over 1,400 stars on GitHub and continues to help Android developers build better forms.