View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  
19  package net.sf.valjax;
20  
21  import org.apache.commons.validator.GenericValidator;
22  import org.springframework.validation.Errors;
23  import org.springframework.validation.Validator;
24  
25  import java.util.Locale;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  /***
30   * Validator that provides some basic validation patterns to use.
31   *
32   * <p>Concrete implementations of this class need to follow the contract
33   * stated in the {@link Validator}  interface.</p>
34   *
35   * @author Zach Legein
36   * @see org.springframework.validation.Validator
37   */
38  public abstract class AbstractValidator implements Validator {
39  
40    /*** Regular Expression that can be used to validate an address pattern */
41    public static final String PATTERN_ADDRESS = "^[a-zA-Z//d]+(([-',. #][a-zA-Z//d ])?[a-zA-Z//d]*[.]*)*$";
42  
43    /*** Regular Expression that can be used to validate alphabetic patterns */
44    public static final String PATTERN_ALPHA = "^[a-zA-Z]+$";
45  
46    /*** Regular Expression that can be used to validate an alpha numeric patterns */
47    public static final String PATTERN_ALPHANUMERIC = "^[//w+0-9]+$";
48  
49    /***
50     * Regular Expression that can be used to validate an currency patterns.
51     *
52     * <p>For example, <code>$12.45</code> is a valid pattern.</p>
53     */
54    public static final String PATTERN_CURRENCY =
55      "^-?//$?((//d{1,3}(//,//d{3})*|//d+)(//.//d{0,2})?)$|^-?//$?//.//d{1,2}?$";
56  
57    /***
58     * Regular Expression that can be used to validate date patterns.
59     *
60     * <p><code>mm/dd/yyyy</code></p>
61     *
62     */
63    public static final String PATTERN_DATE = "^//d{1,2}///d{1,2}///d{4}$";
64  
65    /***
66     * Regular Expression that can be used to validate numeric patterns.
67     *
68     * <p>This is numeric only and doesn't include additiona syntax.</p>
69     *
70     * <p><code>1,233,434.09</code> is not allowed.</p>
71     */
72    public static final String PATTERN_DIGITS = "^//d+$";
73  
74    /***
75     * Regular Expression that can be used to validate a phone pattern.
76     *
77     * <p>Pattern is without any additional syntax, this is strictly:</p>
78     *
79     * <p><code>123-123-1233<code> or <code>1234567890</code></p>
80     */
81    public static final String PATTERN_FAX = "^//d{3}-//d{3}-//d{4}|//d{10}$";
82  
83    /***
84     * Regular Expression that can be used to validate the month.
85     *
86     * <p><code>1-12</code> or <code>01-09, 10, 11, 12</code></p>
87     */
88    public static final String PATTERN_MONTH = "^0?[1-9]$|^1[0-2]$";
89  
90    /***
91     * Regular Expression that can be used to validate the month and year.
92     *
93     * <p>The month being the first in the pattern.</p>
94     *
95     * <p><code>1-12/2006</code> or <code>01-09, 10, 11, 12/2006</code></p>
96     */
97    public static final String PATTERN_MONTH_YEAR = "^(0?[1-9]|1[0-2])///d{4}$";
98  
99    /***
100    * Regular Expression that can be used to validate patterns
101    * any number with numeric syntax such as:
102    *
103    * <p><code>-1,233,434.09</code></p>
104    */
105   public static final String PATTERN_NUMBER = "^-?((//d{1,3}(//,//d{3})*|//d+)(//.//d*)?)$|^-?//.//d+?$";
106 
107   /***
108    * Regular Expression that can be used to validate a phone pattern
109    * with additional syntax, such as extension numbers:
110    *
111    * <p><code>123-123-1233x12345<code> or <code>1234567890</code></p>
112    */
113   public static final String PATTERN_PHONE = "^(//d{3}-//d{3}-//d{4}|//d{10})( ?[xX] ?//d{1,5})?$";
114 
115   /***
116    * Regular Expression that can be used to validate the year
117    * using 4 digits.
118    *
119    * <p><code>2006</code></p>
120    */
121   public static final String PATTERN_YEAR = "^//d{4}$";
122 
123   /***
124    * Regular Expression that can be used to validate the zip
125    *
126    * <p>Pattern checks for any addtional formats</p>
127    *
128    * <p><code>90404-1234, 90404, 904041234</code> are allowed</p>
129    */
130   public static final String PATTERN_ZIP = "^//d{5}(-?//d{4})?$";
131 
132   /***
133    * Regular Expression that can be used to validate a name.
134    *
135    * <p>This may contain charatercs such as, '-',
136    * for names like 'Wu-Tang'</p>
137    */
138   public static final Pattern NAME_PATTERN_2 = Pattern.compile("^([a-zA-Z '-]+)$");
139 
140   /*** Regular Expression that can be used to validate a name */
141   public static final Pattern NAME_PATTERN_1 = Pattern.compile("^.*[B-Db-dF-Hf-hJ-Nj-nP-Tp-tV-Xv-xZz]{6,}.*$");
142 
143   /***
144    * Uses the name regex pattern to access whether this
145    * string value is a match
146    *
147    * @param value The input to check.
148    * @return boolean if value matches patterns or not.
149    * @see #NAME_PATTERN_1
150    * @see #NAME_PATTERN_2
151    */
152   public Boolean isFamilyName(String value) {
153     Boolean ret      = false;
154     Matcher matcher1 = NAME_PATTERN_1.matcher(value);
155     Matcher matcher2 = NAME_PATTERN_2.matcher(value);
156 
157     // person with a single character lastname :-)
158     if (!matcher1.matches() && matcher2.matches() && matcher2.matches()) {
159       ret = true;
160     }
161 
162     return ret;
163   }
164 
165   /***
166    * Uses the name regex pattern to access whether this
167    * string value is a match
168    *
169    * @param value The input to check.
170    * @return Whether value matches pattern or not.
171    * @see #NAME_PATTERN_1
172    * @see #NAME_PATTERN_2
173    */
174   public Boolean isGivenName(String value) {
175     Boolean ret      = false;
176     Matcher matcher1 = NAME_PATTERN_1.matcher(value);
177     Matcher matcher2 = NAME_PATTERN_2.matcher(value);
178 
179     if (!matcher1.matches() && matcher2.matches()) {
180       ret = true;
181     }
182 
183     return ret;
184   }
185 
186   /***
187    * Validate a value against your own defined pattern.
188    * Pattern must be written in a valid regular expression.
189    *
190    * @param value The input to check
191    * @param pattern The pattern to match with
192    * @return Whether value matches pattern or not.
193    */
194   public Boolean validateCustom(String value, String pattern) {
195     return Pattern.matches(pattern, value);
196   }
197 
198   /***
199    * Uses the address regex pattern to access whether this
200    * string value is a match
201    *
202    * @param value The input to check.
203    * @return Whether value matches pattern or not.
204    * @see #PATTERN_ADDRESS
205    */
206   public Boolean isAddress(String value) {
207     return validateCustom(value, PATTERN_ADDRESS);
208   }
209 
210   /***
211    * Uses the alphabetic regex pattern to access whether this
212    * string value is a match
213    *
214    * @param value The input to check.
215    * @return Whether value matches pattern or not.
216    * @see #PATTERN_ALPHA
217    */
218   public Boolean isAlpha(String value) {
219     return validateCustom(value, PATTERN_ALPHA);
220   }
221 
222   /***
223    * Uses the alpha numeric regex pattern to access whether this
224    * string value is a match
225    *
226    * @param value The input to check.
227    * @return Whether value matches pattern or not.
228    * @see #PATTERN_ALPHANUMERIC
229    */
230   public Boolean isAlphaNumeric(String value) {
231     return validateCustom(value, PATTERN_ALPHANUMERIC);
232   }
233 
234   /***
235    * Uses the alpha numeric regex pattern to access whether this
236    * string value is a match
237    *
238    * @param value The input to check.
239    * @return Whether value matches pattern or not.
240    * @see #PATTERN_ALPHANUMERIC
241    */
242   public Boolean isCurrency(String value) {
243     return validateCustom(value, PATTERN_CURRENCY);
244   }
245 
246   /***
247    * Uses the alpha numeric regex pattern to access whether this
248    * string value is a match
249    *
250    * @param value The input to check.
251    * @return Whether value matches pattern or not.
252    * @see #PATTERN_ALPHANUMERIC
253    */
254   public Boolean isDate(String value) {
255     return GenericValidator.isDate(value, Locale.US) && validateCustom(value, PATTERN_DATE);
256   }
257 
258   /***
259    *  Uses the digits regex pattern to access whether this
260    *  string value is a match
261    *
262    *  @param value The input to check.
263    *  @return Whether value matches pattern or not.
264    *  @see #PATTERN_DIGITS
265    */
266   public Boolean isDigits(String value) {
267     return validateCustom(value, PATTERN_DIGITS);
268   }
269 
270   /***
271    * Uses the fax regex pattern to access whether this
272    * string value is a match
273    *
274    * @param value The input to check.
275    * @return Whether value matches pattern or not.
276    * @see #PATTERN_FAX
277    */
278   public Boolean isFax(String value) {
279     return validateCustom(value, PATTERN_FAX);
280   }
281 
282   /***
283    * Validate month against format mm
284    *
285    * @param value The input to check.
286    * @return Whether value matches pattern or not.
287    * @see #PATTERN_MONTH
288    */
289   public Boolean isMonth(String value) {
290     return validateCustom(value, PATTERN_MONTH);
291   }
292 
293   /***
294    * Validate month/year against format mm/yyyy
295    *
296    * @param value The input to check.
297    * @return Whether value matches pattern or not.
298    * @see #PATTERN_MONTH_YEAR
299    */
300   public Boolean isMonthYear(String value) {
301     return validateCustom(value, PATTERN_MONTH_YEAR);
302   }
303 
304   /***
305    * Uses the phone regex pattern to access whether this
306    * string value is a match
307    *
308    * <p>Formats:  -#####.###  -##,###.###</p>
309    *
310    * @param value The input to check.
311    * @return Whether value matches pattern or not.
312    * @see #PATTERN_NUMBER
313    */
314   public Boolean isNumber(String value) {
315     return validateCustom(value, PATTERN_NUMBER);
316   }
317 
318   /***
319    * Uses the phone regex pattern to access whether this
320    * string value is a match
321    *
322    * @param value The input to check.
323    * @return Whether value matches pattern or not.
324    * @see #PATTERN_PHONE
325    */
326   public Boolean isPhone(String value) {
327     return validateCustom(value, PATTERN_PHONE);
328   }
329 
330   /***
331    * Uses the year regex pattern to access whether this
332    * string value is a match
333    *
334    * @param value The input to check.
335    * @return Whether value matches pattern or not.
336    * @see #PATTERN_YEAR
337    */
338   public Boolean isYear(String value) {
339     return validateCustom(value, PATTERN_YEAR);
340   }
341 
342   /***
343    * Uses the zip code regex pattern to access whether this
344    * string value is a match
345    *
346    * @param value The input to check.
347    * @return Whether value matches pattern or not.
348    * @see #PATTERN_ZIP
349    */
350   public Boolean isZip(String value) {
351     return validateCustom(value, PATTERN_ZIP);
352   }
353 }