KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > PhoneTypeValidator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator;
8
9
10 import java.util.Locale JavaDoc;
11
12
13 /**
14  * <p>
15  * This class is a phone number validator that conforms to
16  * the Inversoft validation API. This Validator checks that
17  * the phone conforms to standard 7, 10 or 11 digit US
18  * telephone numbers.
19  * </p>
20  *
21  * @author Brian Pontarelli
22  * @version 2.0
23  * @since 2.0
24  */

25 public class PhoneTypeValidator extends BaseTypeValidator {
26
27     /**
28      * Default error message returned from validate methods (inside Basic and
29      * PropertyError objects)
30      */

31     public static final String JavaDoc DEFAULT_MESSAGE = "Invalid phone number";
32
33
34     /**
35      * Constructs a new <code>PhoneTypeValidator</code>
36      */

37     public PhoneTypeValidator() {
38         // Empty
39
}
40
41
42     /**
43      * Does the work of validating the phone number. If the Locale is US, this
44      * validates US phone numbers. More will be added later.
45      *
46      * @param value The value to validate
47      * @param params (Optional) An Object that can be used to pass parameters
48      * to a validator that can help determine how to validation is to
49      * take place
50      * @param message The error message to returnif the validation fails
51      * @param locale (Optional) The Locale of the bundle. If null, the default
52      * Locale is used
53      * @param mesgParams (Optional) An array of Objects that is passed to the
54      * java.text.MessageFormat class to format the message passed in.
55      * If this is null, no formatting takes place
56      * @return The error message supplied and formatted (if mesgParams is not
57      * null) or null if the validation was successfull
58      */

59     protected String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
60             Locale JavaDoc locale, Object JavaDoc [] mesgParams) {
61         if (locale == null) {
62             locale = Locale.getDefault();
63         }
64
65         String JavaDoc 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     /**
74      * Does the actual work of validation and error message formatting. This also
75      * checks if the message is null and uses the DEFAULT_MESSAGE
76      */

77     private String JavaDoc internalValidateUS(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
78             Object JavaDoc [] mesgParams) {
79         String JavaDoc error = null;
80
81         // Do the validation of the phone number
82
StringBuffer JavaDoc numberBuf = new StringBuffer JavaDoc();
83         boolean valid = true;
84         if (value != null) {
85             String JavaDoc 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 JavaDoc prefix = null;
109                 String JavaDoc area = null;
110                 if (length == 7) {
111                     area = "303"; // Anything valid is okay here
112
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