项目作者: Gauravd70

项目描述 :
Android EditText input validation library
高级语言: Java
项目地址: git://github.com/Gauravd70/EditTextValidator.git
创建时间: 2020-05-28T07:00:11Z
项目社区:https://github.com/Gauravd70/EditTextValidator

开源协议:BSD 2-Clause "Simplified" License

下载


EditTextValidator

EditTextValidator is a library for Android which helps in input validation using regular expressions.

Features

  • Easy to use
  • Input validation using regular expressions
  • Has basic predefined regular expressions for form validation
  • Can be used to compare one Edittext with another
  • Has high flexibility allowing the use of custom ui components as backgrounds or drawables
  • Has Timeout feature which waits till the desired time before validation
  • Provides automatic input validation

Prerequisites

  1. The library makes use of Java8. Add the following to your app build.gradle if not present.
    1. android{
    2. compileOptions{
    3. sourceCompatibility JavaVersion.VERSION_1_8
    4. targetCompatibility JavaVersion.VERSION_1_8
    5. }
    6. }
  2. The library is built on AndroidX. So, for successful compilation migrate to AndroidX. Steps of migration :-
    • Click on Refactor on the menubar
    • At the bottom part of the Refactor menu click on Migrate to AndriodX

Adding dependency

  1. Add this in your root build.gradle

    1. allprojects {
    2. repositories {
    3. maven { url 'https://jitpack.io' }
    4. }
    5. }
  2. Add the dependency to your app build.gradle

    1. dependencies{
    2. implementation 'com.github.Gauravd70:EditTextValidator:1.0.2'
    3. }

Additional Attributes of ValidatorEditText

  1. app:checkType=”” - Used to define the type of validation. By default is alpha.

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:checkType="email"></com.gd70.android.validator.ValidatorEditText>
  2. app:validBackground=”” - Background drawable to be used if input is valid

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:validBackground="@drawable/validBackgroundDrawable"></com.gd70.android.validator.ValidatorEditText>
  3. app:invalidBackground=”” - Background drawable to be used if input is invalid

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:invalidBackground="@drawable/invalidBackgroundDrawable"></com.gd70.android.validator.ValidatorEditText>
  4. app:validDrawable=”” - Drawable to be used if input is valid (This is shown inside the EditText)

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:validDrawable="@drawable/validDrawable"></com.gd70.android.validator.ValidatorEditText>
  5. app:invalidDrawable=”” - Drawable to be used if input is invalid (This is shown inside the EditText)

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:invalidDrawable="@drawable/invalidDrawable"></com.gd70.android.validator.ValidatorEditText>
  6. app:drawablePosition=”” - Location of the drawable inside the edittext. By default is Right(end) of the edittext

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:drawablePosition="left"></com.gd70.android.validator.ValidatorEditText>
  7. app:useRegex=”” - provide a custom regex to be used for validation

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:useRegex="[0-9]+"></com.gd70.android.validator.ValidatorEditText>
  8. app:timeOut=”” - wait time(in milliseconds) before validation is started. By default is 500ms

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:timeOut="1000"></com.gd70.android.validator.ValidatorEditText>
  9. app:compareTo=”” - used to compare one edittext with another

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:compareTo="@id/password"></com.gd70.android.validator.ValidatorEditText>
  10. app:required=”” - mark a field as required

    1. <com.gd70.android.validator.ValidatorEditText
    2. .
    3. .
    4. app:required="true"></com.gd70.android.validator.ValidatorEditText>
  11. To check for the validity on form submission you can use the following function

    1. ValidatorEditText validatorEditText=findViewById(R.id.validatorText);
    2. if(validatorEditText.isValid()){
    3. //valid
    4. }
    5. else{
    6. //invalid
    7. }

How to use

  1. Use ValidatorEditText instead of EditText

    1. <com.gd70.android.validator.ValidatorEditText
    2. android:layout_width="match_parent"
    3. android:layout_height="wrap_content"
    4. app:layout_constraintTop_toTopOf="parent"
    5. android:textSize="20sp"
    6. android:padding="10sp"
    7. android:hint="@string/username"
    8. ></com.gd70.android.validator.ValidatorEditText>
  2. Define the validation type using checkType attribute or provide your own regular expression to the useRegex attribute