You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
An extension to android design support library's TextInputLayout with validation support
Demo
Note: v1.0.0-beta1 adds migration to androidx and to kotlin from java. Upgrading to beta mightbreak your code.
Features
AutoValidation Validate the input field as the text changes. input.autoValidate(true)Iffalse you need to call thevalidate() method explicitly for validation. OR use xml attributeautoValidate as true or false.
AutoTrim input.getValue() will return the value of input field after removing leading and trailingwhite spaces
input.autoTrimValue(true)OR use xml attributeautoTrim as true or false.
Add Validators You can add multiple validators to a single input field. input.addValidator(/* Your first Validator class goes here */)input.addValidator(/* Your second Validator class goes here */)
Clear Validators Removes all the validators associated with the input field. input.clearValidators()
Default Available Validators
RequiredValidator Validates the input field as required. i.e. empty value is not valid. input.addValidator(RequiredValidator("Your error message"))OR use xml attributeisRequired as true or false. The default message will be "This field is required." For custom message you can use xml attributerequiredValidationMessage
LengthValidator Validates the input field against minimum and maximum length specified. input.addValidator(LengthValidator(8 /* Max Length */, "Your error message"))input.addValidator(LengthValidator(4 /* Min Length */, *8 /* Max Length */, "Your error message"))OR use xml attributesminLength andmaxLength with default values being "zero" and "indefinite" respectively. The default message will be one of following
The input must have length between "minLength" and "maxLength".
The input length must be greater than or equal to "minLength".
The input length must be less than or equal to "maxLength". based on your values forminLength andmaxLength attributes. For custom message you can use xml attributelengthValidationMessage
RegexValidator Validates the input field against provided regular expression. Equivalent toString.matches() input.addValidator(new RegexValidator("your_regex", "Your error message"))OR use xml attributeregex to set your regular expression. The default message will be "The field value does not match the required format." For custom message you can use xml attributeregexValidationMessage
DependencyValidator Validates the input field as per the dependency type with the input field it dependson. Ifinput1 depends oninput2 with dependency type TYPE_EQUAL: (i.e.input1 .getValue() must be equal toinput1.getValue()) input1.addValidator(new DependencyValidator(input2, TYPE_EQUAL, "Your error message"))
Custom Validators You can create your own validators to use with ValidatedTextInputLayout just by extending theBaseValidator class. You need to call thesuper() method with the desired message and overrideisValid() method to return true or false;
Example: Validator class to check if field value contains character sequence "xyz"
class MyValidator(errorMessage: String, callback: ValidationCallback? = null): BaseValidator(errorMessage, callback) { override fun isValid(text: String): Boolean { return text.contains("xyz") } }
Validation CallbackYou can add callbacks to the validators from constructor or a setter using theValidationCallback class.
Copyright 2016 Julian Raj ManandharLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
About
An extension to Android Design Support library's TextInputLayout with integrated validation support