I want to figure out invalid phone numbers entered in edittext before hitting the OTP api. Invalid phone numbers pattern may looks like these patterns +91-0000000000 / +91-0000000011 / +91-1234567890 ...and so on. Already added a check to validate invalid phone number but it didn't helps in detecting invalid patterns of phone numbers which can't exist.
private boolean isValidMobile(String phone) {
return android.util.Patterns.PHONE.matcher(phone).matches();
}
Anyone please suggest how to handle these numbers before hitting the OTP api. Thanks in advance.
To validate a 10 digit Mobile number, here is a working code. Hope this works for you too.
String number = binding.editNumber.getText().toString();
// isPhoneValid = isValidMobileNo(number);
if (isValidMobileNo(number)) {
isPhoneValid = true;
else {
Log.d("Result: ","Not Valid");
isPhoneValid = false;
valid = false;
}
private boolean isValidMobileNo(String str) {
Pattern pattern = Pattern.compile("(0/91)?[6-9][0-9]{9}");
Matcher match = pattern.matcher(str);
return (match.find() && match.group().equals(str));
}
Test Sample can be seen [ here ], an error message set to the EditText can be seen in this example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.