How can i create my own editText with customised validation types?

226 views Asked by At

I want to create my own edit text field ,which is used for multiple entries from the user with different set of required validations(email,phone,credit card,mixed chars).

1

There are 1 answers

0
Giridharan On

Try like this:

For CreditCard:

 public boolean isValid(EditText et) {
    try {
      return validateCardNumber(et.getText().toString());
    } catch (Exception e) {
      return false;
    }
  }
 public static boolean validateCardNumber(String cardNumber) throws NumberFormatException {
    int sum = 0, digit, addend = 0;
    boolean doubled = false;
    for (int i = cardNumber.length () - 1; i >= 0; i--) {
      digit = Integer.parseInt (cardNumber.substring (i, i + 1));
      if (doubled) {
        addend = digit * 2;
        if (addend > 9) {
          addend -= 9; 
        }
      } else {
        addend = digit;
      }
      sum += addend;
      doubled = !doubled;
    }
    return (sum % 10) == 0;
  }

For IpAddress:

public IpAddressValidator(String _customErrorMessage) {
    super(_customErrorMessage, Build.VERSION.SDK_INT>=8?Patterns.IP_ADDRESS:Pattern.compile(
            "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
            + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
            + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
            + "|[1-9][0-9]|[0-9]))"));
}

Check this example application for more details:here