Android EditText input validation library
EditTextValidator is a library for Android which helps in input validation using regular expressions.
android{
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Add this in your root build.gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Add the dependency to your app build.gradle
dependencies{
implementation 'com.github.Gauravd70
1.0.2'
}
app:checkType=”” - Used to define the type of validation. By default is alpha.
<com.gd70.android.validator.ValidatorEditText
.
.
app:checkType="email"></com.gd70.android.validator.ValidatorEditText>
app:validBackground=”” - Background drawable to be used if input is valid
<com.gd70.android.validator.ValidatorEditText
.
.
app:validBackground="@drawable/validBackgroundDrawable"></com.gd70.android.validator.ValidatorEditText>
app:invalidBackground=”” - Background drawable to be used if input is invalid
<com.gd70.android.validator.ValidatorEditText
.
.
app:invalidBackground="@drawable/invalidBackgroundDrawable"></com.gd70.android.validator.ValidatorEditText>
app:validDrawable=”” - Drawable to be used if input is valid (This is shown inside the EditText)
<com.gd70.android.validator.ValidatorEditText
.
.
app:validDrawable="@drawable/validDrawable"></com.gd70.android.validator.ValidatorEditText>
app:invalidDrawable=”” - Drawable to be used if input is invalid (This is shown inside the EditText)
<com.gd70.android.validator.ValidatorEditText
.
.
app:invalidDrawable="@drawable/invalidDrawable"></com.gd70.android.validator.ValidatorEditText>
app:drawablePosition=”” - Location of the drawable inside the edittext. By default is Right(end) of the edittext
<com.gd70.android.validator.ValidatorEditText
.
.
app:drawablePosition="left"></com.gd70.android.validator.ValidatorEditText>
app:useRegex=”” - provide a custom regex to be used for validation
<com.gd70.android.validator.ValidatorEditText
.
.
app:useRegex="[0-9]+"></com.gd70.android.validator.ValidatorEditText>
app:timeOut=”” - wait time(in milliseconds) before validation is started. By default is 500ms
<com.gd70.android.validator.ValidatorEditText
.
.
app:timeOut="1000"></com.gd70.android.validator.ValidatorEditText>
app:compareTo=”” - used to compare one edittext with another
<com.gd70.android.validator.ValidatorEditText
.
.
app:compareTo="@id/password"></com.gd70.android.validator.ValidatorEditText>
app:required=”” - mark a field as required
<com.gd70.android.validator.ValidatorEditText
.
.
app:required="true"></com.gd70.android.validator.ValidatorEditText>
To check for the validity on form submission you can use the following function
ValidatorEditText validatorEditText=findViewById(R.id.validatorText);
if(validatorEditText.isValid()){
//valid
}
else{
//invalid
}
Use ValidatorEditText instead of EditText
<com.gd70.android.validator.ValidatorEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:textSize="20sp"
android:padding="10sp"
android:hint="@string/username"
></com.gd70.android.validator.ValidatorEditText>
Define the validation type using checkType attribute or provide your own regular expression to the useRegex attribute