GithubHelp home page GithubHelp logo

form's Introduction

Form

An Android library supports validating some types of EditText:

  • Email
  • Password (with FormEditText instead of EditText)
  • Phone
  • URL

Demo

Alt Text

Installation

Step 1: Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2: Add the dependency

implementation 'com.github.thaitv21:Form:Tag'

Well, now you can use it.

Usage

There are many ways to validate input value.

Option 1: Use Validation

new Validation.Builder().addAll(edtEmail, edtPhone, edtPassword).build().validate(new Callback() {
    @Override
    public void callback(boolean isSuccessful, List<EditText> invalidInputs, List<EditText> emptyInputs) {
        StringBuilder sb = new StringBuilder();
        sb.append("EMPTY: ");
        for (EditText edt : emptyInputs) {
            sb.append(edt.getHint());
            sb.append(", ");
        }
        sb.append("\nINVALID: ");
        for (EditText edt : invalidInputs) {
            sb.append(edt.getHint());
            sb.append(", ");
        }
        Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
    }
});

Moreover, your Builder instance can do the following methods:

add(EditText editText)
add(EditText editText, String regex, boolean allowEmpty, boolean trim)
add(EditText editText, String regex, boolean allowEmpty)
add(EditText editText, boolean allowEmpty)
add(EditText editText, String regex)
addAll(EditText... editTexts)
addAll(List<EditText> editTexts)

Option 2: Use FormLinearLayout

FormLinearLayout is an extended LinearLayout. In order to validate all of EditText, you just wrap your layout with a element like this:

<com.thaitv.form.FormLinearLayout
    android:id="@+id/formlinear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="email"/>
        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="password"/>
    </FrameLayout>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:hint="phone"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="validateEverything"
        android:text="Validate"/>

</com.thaitv.form.FormLinearLayout>
FormLinearLayout formlinear = findViewById(R.id.formlinear);
formlinear.validate(new Callback() {
    @Override
    public void callback(boolean isSuccessful, List<EditText> invalidInputs, List<EditText> emptyInputs) {
        StringBuilder sb = new StringBuilder();
        sb.append("EMPTY: ");
        for (EditText edt : emptyInputs) {
            sb.append(edt.getHint());
            sb.append(", ");
        }
        sb.append("\nINVALID: ");
        for (EditText edt : invalidInputs) {
            sb.append(edt.getHint());
            sb.append(", ");
        }
        Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
    }
})

As you see, I don't need any instance of EditText.

Option 3: For any layout

It's helpful if you don't want to make your layout more staked. For any layout, like this:

<LinearLayout
   android:id="@+id/linearlayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="email"/>
   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="password"/>

   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="validateEverything"
        android:text="Validate"/>
</LinearLayout>
LinearLayout linearLayout = findViewById(R.id.linearlayout);
ValidationUtils.validate(linearLayout, new Callback() {
    @Override
    public void callback(boolean isSuccessful, List<EditText> invalidInputs, List<EditText> emptyInputs) {
         StringBuilder sb = new StringBuilder();
         sb.append("EMPTY: ");
         for (EditText edt : emptyInputs) {
              sb.append(edt.getHint());
              sb.append(", ");
         }
         sb.append("\nINVALID: ");
         for (EditText edt : invalidInputs) {
              sb.append(edt.getHint());
              sb.append(", ");
         }
         Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
     }
});

Develop by

Than Thai
Contact: [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.