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).
Asked
Active
Viewed 214 times
-1
-
1You can set up the validation such as email, phone, etc, for different editText for that you do not need any custom EditText – Rajan Bhavsar Jun 15 '15 at 06:33
-
possible duplicate of [Android: How can I validate EditText input?](http://stackoverflow.com/questions/2763022/android-how-can-i-validate-edittext-input) – Ivan Aksamentov - Drop Jun 15 '15 at 06:34
-
i want more validations like credit card number ,ip address,url validations like...and also i want to select those customised validation types from xml. – Hari Enaganti Jun 15 '15 at 06:41
1 Answers
0
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

Giridharan
- 708
- 7
- 16