Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
- PhoneNumber numberProto = phoneUtil.parse("phone_number", "");
- phoneUtil.isValidNumber(numberProto) == true ? "valid" : "phone no not valid"
- if (setNum.matches(regexStr))
- where regexStr can be:
- //matches numbers only
- String regexStr = "^[0-9]*$"
- //matches 10-digit numbers only
- String regexStr = "^[0-9]{10}$"
- //matches numbers and dashes, any order really.
- String regexStr = "^[0-9\-]*$"
- //matches 9999999999, 1-999-999-9999 and 999-999-9999
- String regexStr = "^(1\-)?[0-9]{3}\-?[0-9]{3}\-?[0-9]{4}$"
- String regexStr = "^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$"
- public static boolean isValidPhoneNo(CharSequence iPhoneNo) {
- return !TextUtils.isEmpty(iPhoneNo) &&
- Patterns.PHONE.matcher(iPhoneNo).matches();
- }
- String number = "012-1234567";
- Pattern pattern = Pattern.compile("\d{3}-\d{7}");
- Matcher matcher = pattern.matcher(number);
- if (matcher.matches()) {
- System.out.println("Phone Number Valid");
- }
- /**
- * This method is used to set filter type of us phone number.
- * @param phone
- */
- public static void setFilterTypeOfUSPhoneNumber(final TextView phone){
- InputFilter filter = new InputFilter() {
- public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
- String pattern = "0123456789()- ";
- for (int i = start; i < end; i++) {
- if (pattern.indexOf(source.charAt(i)) < 0 ||
- source.length() > 14) {
- return "";
- }
- }
- return null;
- }
- };
- phone.setFilters(new InputFilter[]{filter ,new InputFilter.LengthFilter(14)});
- phone.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {}
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
- @Override
- public void afterTextChanged(Editable s) {
- if(s.length() > 1){
- if(s.length() < 5){
- if(s.toString().indexOf("(") != 0 ||
- checkSpecialCharsPositions(s.toString())){
- String str = s.toString();
- str = replaceStrings(str);
- str = "("+str;
- s.clear();
- s.append(str);
- phone.setText(s);
- }
- }
- else if(s.length() < 10){
- if(s.toString().indexOf("(") != 0 ||
- s.toString().indexOf(")") != 4 ||
- checkSpecialCharsPositions(s.toString())){
- String str = s.toString();
- str = replaceStrings(str);
- str = "("+str.substring(0, 3)+") "+str.substring(3);
- s.clear();
- s.append(str);
- phone.setText(s);
- }
- }
- else {
- if(s.toString().indexOf("(") != 0 ||
- s.toString().indexOf(")") != 4 ||
- s.toString().indexOf("-") != 9 ||
- checkSpecialCharsPositions(s.toString())){
- String str = s.toString();
- str = replaceStrings(str);
- str = "("+str.substring(0, 3)+") "+str.substring(3,6) + "-" + str.substring(6);
- s.clear();
- s.append(str);
- phone.setText(s);
- }
- }
- }
- Selection.setSelection(s,s.length());
- }
- private String replaceStrings(String str){
- str = str.replace("(", "");
- str = str.replace(")", "");
- str = str.replace(" ", "");
- str = str.replace("-", "");
- return str;
- }
- private boolean checkSpecialCharsPositions(String str){
- return (str.indexOf("(") != str.lastIndexOf("(") ||
- str.indexOf(")") != str.lastIndexOf(")") ||
- str.indexOf("-") != str.lastIndexOf("-"));
- }
- });
- }
Add Comment
Please, Sign In to add comment