1 7 package com.inversoft.util.typevalidator; 8 9 10 import java.util.Locale ; 11 12 13 25 public class PhoneTypeValidator extends BaseTypeValidator { 26 27 31 public static final String DEFAULT_MESSAGE = "Invalid phone number"; 32 33 34 37 public PhoneTypeValidator() { 38 } 40 41 42 59 protected String internalValidate(Object value, Object params, String message, 60 Locale locale, Object [] mesgParams) { 61 if (locale == null) { 62 locale = Locale.getDefault(); 63 } 64 65 String error = null; 66 if (locale.getCountry().equals("US") || locale.getCountry().equals("UM")) { 67 error = internalValidateUS(value, params, message, mesgParams); 68 } 69 70 return error; 71 } 72 73 77 private String internalValidateUS(Object value, Object params, String message, 78 Object [] mesgParams) { 79 String error = null; 80 81 StringBuffer numberBuf = new StringBuffer (); 83 boolean valid = true; 84 if (value != null) { 85 String localNumber = value.toString().trim(); 86 char ch; 87 for (int i = 0; i < localNumber.length(); i++) { 88 ch = localNumber.charAt(i); 89 if (Character.isDigit(ch)) { 90 numberBuf.append(ch); 91 } else if (ch != '(' && ch != ')' && ch != '.' && ch != '-' && ch != ' ') { 92 valid = false; 93 break; 94 } 95 } 96 } else { 97 valid = false; 98 } 99 100 if (valid) { 101 int length = numberBuf.length(); 102 char ch = numberBuf.charAt(0); 103 104 valid = ((length == 7 && ch != '1') || 105 (length == 10 && ch != '1') || 106 (length == 11 && ch == '1')); 107 if (valid) { 108 String prefix = null; 109 String area = null; 110 if (length == 7) { 111 area = "303"; prefix = numberBuf.substring(0, 3); 113 } else if (length == 10) { 114 area = numberBuf.substring(0, 3); 115 prefix = numberBuf.substring(3, 6); 116 } else { 117 area = numberBuf.substring(1, 4); 118 prefix = numberBuf.substring(4, 7); 119 } 120 121 int areaInt = Integer.parseInt(area); 122 int prefixInt = Integer.parseInt(prefix); 123 valid = (prefixInt >= 200 && prefixInt != 911 && prefixInt != 555 124 && areaInt != 911 && areaInt >= 200); 125 } 126 } 127 128 if (!valid) { 129 error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams); 130 } 131 132 return error; 133 } 134 } | Popular Tags |