How do I check if a phone number is valid or not? It is up to length 13 (including character +
in front).
How do I do that?
I tried this:
String regexStr = "^[0-9]$";
String number=entered_number.getText().toString();
if(entered_number.getText().toString().length()<10 || number.length()>13 || number.matches(regexStr)==false ) {
Toast.makeText(MyDialog.this,"Please enter "+"\n"+" valid phone number",Toast.LENGTH_SHORT).show();
// am_checked=0;
}`
And I also tried this:
public boolean isValidPhoneNumber(String number)
{
for (char c : number.toCharArray())
{
if (!VALID_CHARS.contains(c))
{
return false;
}
}
// All characters were valid
return true;
}
Both are not working.
Input type: + sign to be accepted and from 0-9 numbers and length b/w 10-13 and should not accept other characters
This question is related to
android
validation
phone-number
val UserMobile = findViewById<edittext>(R.id.UserMobile)
val msgUserMobile: String = UserMobile.text.toString()
fun String.isMobileValid(): Boolean {
// 11 digit number start with 011 or 010 or 015 or 012
// then [0-9]{8} any numbers from 0 to 9 with length 8 numbers
if(Pattern.matches("(011|012|010|015)[0-9]{8}", msgUserMobile)) {
return true
}
return false
}
if(msgUserMobile.trim().length==11&& msgUserMobile.isMobileValid())
{//pass}
else
{//not valid}
We can use pattern to validate it.
android.util.Patterns.PHONE
public class GeneralUtils {
private static boolean isValidPhoneNumber(String phoneNumber) {
return !TextUtils.isEmpty(phoneNumber) && android.util.Patterns.PHONE.matcher(phoneNumber).matches();
}
}
I got best solution for international phone number validation and selecting country code below library is justified me Best library for all custom UI and functionality CountryCodePickerProject
You can use android's inbuilt Patterns
:
public boolean validCellPhone(String number)
{
return android.util.Patterns.PHONE.matcher(number).matches();
}
This pattern is intended for searching for things that look like they might be phone numbers in arbitrary text, not for validating whether something is in fact a phone number. It will miss many things that are legitimate phone numbers.
The pattern matches the following:
- Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes may follow.
- Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
- A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
Use isGlobalPhoneNumber()
method of PhoneNumberUtils
to detect whether a number is valid phone number or not.
System.out.println("....g1..."+PhoneNumberUtils.isGlobalPhoneNumber("+912012185234"));
System.out.println("....g2..."+PhoneNumberUtils.isGlobalPhoneNumber("120121852f4"));
The result of first print statement is true while the result of second is false because the second phone number contains f
.
String validNumber = "^[+]?[0-9]{8,15}$";
if (number.matches(validNumber)) {
Uri call = Uri.parse("tel:" + number);
Intent intent = new Intent(Intent.ACTION_DIAL, call);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
return;
} else {
Toast.makeText(EditorActivity.this, "no phone number available", Toast.LENGTH_SHORT).show();
}
To validate phone numbers for a specific region in Android, use libPhoneNumber from Google, and the following code as an example:
public boolean isPhoneNumberValid(String phoneNumber, String countryCode)
{
//NOTE: This should probably be a member variable.
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try
{
PhoneNumber numberProto = phoneUtil.parse(phoneNumber, countryCode);
return phoneUtil.isValidNumber(numberProto);
}
catch (NumberParseException e)
{
System.err.println("NumberParseException was thrown: " + e.toString());
}
return false;
}
you can also check validation of phone number as
/**
* Validation of Phone Number
*/
public final static boolean isValidPhoneNumber(CharSequence target) {
if (target == null || target.length() < 6 || target.length() > 13) {
return false;
} else {
return android.util.Patterns.PHONE.matcher(target).matches();
}
}
^\+201[0|1|2|5][0-9]{8}
this regex matches Egyptian mobile numbers
^\+?\(?[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})?
Check your cases here: https://regex101.com/r/DuYT9f/1
You can use PhoneNumberUtils
if your phone format is one of the described formats. If none of the utility function match your needs, use regular experssions.
You shouldn't be using Regular Expressions when validating phone numbers. Check out this JSON API - numverify.com - it's free for a nunver if calls a month and capable of checking any phone number. Plus, each request comes with location, line type and carrier information.
Here is how you can do it succinctly in Kotlin:
fun String.isPhoneNumber() =
length in 4..10 && all { it.isDigit() }
Source: Stackoverflow.com