Guest User

Untitled

a guest
Feb 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
  2. PhoneNumber numberProto = phoneUtil.parse("phone_number", "");
  3. phoneUtil.isValidNumber(numberProto) == true ? "valid" : "phone no not valid"
  4.  
  5. if (setNum.matches(regexStr))
  6. where regexStr can be:
  7.  
  8. //matches numbers only
  9. String regexStr = "^[0-9]*$"
  10.  
  11. //matches 10-digit numbers only
  12. String regexStr = "^[0-9]{10}$"
  13.  
  14. //matches numbers and dashes, any order really.
  15. String regexStr = "^[0-9\-]*$"
  16.  
  17. //matches 9999999999, 1-999-999-9999 and 999-999-9999
  18. String regexStr = "^(1\-)?[0-9]{3}\-?[0-9]{3}\-?[0-9]{4}$"
  19.  
  20. 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+))?$"
  21.  
  22. public static boolean isValidPhoneNo(CharSequence iPhoneNo) {
  23. return !TextUtils.isEmpty(iPhoneNo) &&
  24. Patterns.PHONE.matcher(iPhoneNo).matches();
  25. }
  26.  
  27. String number = "012-1234567";
  28. Pattern pattern = Pattern.compile("\d{3}-\d{7}");
  29. Matcher matcher = pattern.matcher(number);
  30. if (matcher.matches()) {
  31. System.out.println("Phone Number Valid");
  32. }
  33.  
  34. /**
  35. * This method is used to set filter type of us phone number.
  36. * @param phone
  37. */
  38. public static void setFilterTypeOfUSPhoneNumber(final TextView phone){
  39.  
  40. InputFilter filter = new InputFilter() {
  41. public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  42. String pattern = "0123456789()- ";
  43. for (int i = start; i < end; i++) {
  44. if (pattern.indexOf(source.charAt(i)) < 0 ||
  45. source.length() > 14) {
  46. return "";
  47. }
  48. }
  49. return null;
  50. }
  51. };
  52.  
  53. phone.setFilters(new InputFilter[]{filter ,new InputFilter.LengthFilter(14)});
  54. phone.addTextChangedListener(new TextWatcher() {
  55. @Override
  56. public void onTextChanged(CharSequence s, int start, int before, int count) {}
  57.  
  58. @Override
  59. public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
  60.  
  61. @Override
  62. public void afterTextChanged(Editable s) {
  63.  
  64. if(s.length() > 1){
  65.  
  66. if(s.length() < 5){
  67. if(s.toString().indexOf("(") != 0 ||
  68. checkSpecialCharsPositions(s.toString())){
  69. String str = s.toString();
  70. str = replaceStrings(str);
  71. str = "("+str;
  72. s.clear();
  73. s.append(str);
  74. phone.setText(s);
  75.  
  76. }
  77. }
  78. else if(s.length() < 10){
  79. if(s.toString().indexOf("(") != 0 ||
  80. s.toString().indexOf(")") != 4 ||
  81. checkSpecialCharsPositions(s.toString())){
  82. String str = s.toString();
  83. str = replaceStrings(str);
  84. str = "("+str.substring(0, 3)+") "+str.substring(3);
  85. s.clear();
  86. s.append(str);
  87. phone.setText(s);
  88. }
  89. }
  90. else {
  91. if(s.toString().indexOf("(") != 0 ||
  92. s.toString().indexOf(")") != 4 ||
  93. s.toString().indexOf("-") != 9 ||
  94. checkSpecialCharsPositions(s.toString())){
  95.  
  96. String str = s.toString();
  97. str = replaceStrings(str);
  98. str = "("+str.substring(0, 3)+") "+str.substring(3,6) + "-" + str.substring(6);
  99. s.clear();
  100. s.append(str);
  101. phone.setText(s);
  102. }
  103. }
  104. }
  105. Selection.setSelection(s,s.length());
  106. }
  107.  
  108. private String replaceStrings(String str){
  109. str = str.replace("(", "");
  110. str = str.replace(")", "");
  111. str = str.replace(" ", "");
  112. str = str.replace("-", "");
  113. return str;
  114. }
  115.  
  116. private boolean checkSpecialCharsPositions(String str){
  117.  
  118. return (str.indexOf("(") != str.lastIndexOf("(") ||
  119. str.indexOf(")") != str.lastIndexOf(")") ||
  120. str.indexOf("-") != str.lastIndexOf("-"));
  121. }
  122. });
  123.  
  124. }
Add Comment
Please, Sign In to add comment