1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.validator.xwork2.field;
23
24 import com.opensymphony.xwork2.validator.ValidationException;
25
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29
30
31
32
33
34
35
36
37
38
39 public class FrenchPostCodeFieldValidator extends NuitonFieldValidatorSupport {
40
41 protected static String POST_CODE_REGEXP =
42 "^((0[1-9])|([1-8][0-9])|(9[0-8])|(2A)|(2B))[0-9]{3}$";
43
44 protected static Pattern p = Pattern.compile(POST_CODE_REGEXP);
45
46 @Override
47 public void validateWhenNotSkip(Object object) throws ValidationException {
48
49 String fieldName = getFieldName();
50 Object value = getFieldValue(fieldName, object);
51
52 if (value == null) {
53
54 return;
55 }
56 if (value instanceof String) {
57 if ("".equals(value)) {
58
59 return;
60 }
61 Matcher m = p.matcher((String) value);
62 if (!m.matches()) {
63 addFieldError(fieldName, object);
64 }
65 } else {
66 addFieldError(fieldName, object);
67 }
68 }
69
70 @Override
71 public String getValidatorType() {
72 return "frenchPostCode";
73 }
74 }