rengwuxian/MaterialEditText — pull request #29 by hzsweers (2014-12-02 10:54:39+00:00)
Title: Custom validation interface

This pull request adds custom validation options via a new `METValidator` interface. This is inspired by [this great library](https://github.com/vekexasia/android-edittext-validator), and could easily be expanded upon to match that one's number 1st party validators.

This just adds two options:
* `METValidator`, which is the basic abstract class with an abstract `isValid()` method that the user implements
* `RegexValidator` a validator for regexes, and also serving as an example implementation extending from `METValidator`

This adds a lot of flexibility to the validation to support more than just regexes.

This also supports adding multiple validators, if necessary.

### Examples

`RegexpValidator` / simple use of `addValidator(...)`

```java
final MaterialEditText et = new MaterialEditText(this);
et.addValidator(new RegexpValidator("Only Integer Valid!", "\\d+"));
final Button validateBt = (Button) findViewById(R.id.validateBt);
validateBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// validate
et.validate();
}
});
```

One-off validation with `validateWith(...)`:

```java
final MaterialEditText et = new MaterialEditText(this);
final Button validateBt = (Button) findViewById(R.id.validateBt);
validateBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// validate
et.validateWith(new RegexValidator("Only Integer Valid!", "\\d+"));
}
});
```

Complex:

```java
final MaterialEditText et = new MaterialEditText(this);
et.addValidator(new CustomValidator1())
.addValidator(new CustomValidator2())
.addValidator(new CustomValidator3());

et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
// Validate field after text changed
et.validate();
}
});
```