private boolean isPhoneNumberValid(String phoneNumber) {
boolean isValid = false;
/**
* valid phone number format;
*/
String expression1 = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
/**
* valid phone number format;
*/
String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
CharSequence inputStr = phoneNumber;
Pattern pattern1 = Pattern.compile(expression1);
Matcher matcher1 = pattern1.matcher(inputStr);
Pattern pattern2 = Pattern.compile(expression2);
Matcher matcher2 = pattern2.matcher(inputStr);
if (matcher1.matches() || matcher2.matches()) {
isValid = true;
}
return isValid;
}
boolean isValid = false;
/**
* valid phone number format;
*/
String expression1 = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
/**
* valid phone number format;
*/
String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
CharSequence inputStr = phoneNumber;
Pattern pattern1 = Pattern.compile(expression1);
Matcher matcher1 = pattern1.matcher(inputStr);
Pattern pattern2 = Pattern.compile(expression2);
Matcher matcher2 = pattern2.matcher(inputStr);
if (matcher1.matches() || matcher2.matches()) {
isValid = true;
}
return isValid;
}