1 20 package org.enhydra.barracuda.core.forms.validators; 21 22 import java.util.*; 23 24 import org.enhydra.barracuda.core.forms.*; 25 import org.enhydra.barracuda.plankton.*; 26 import org.apache.log4j.*; 27 28 66 public class DigitValidator extends DefaultFormValidator { 67 68 protected int len = 0; 69 protected boolean requireLength = false; 70 protected boolean phoneNumber = false; 71 protected boolean domesticPhone = false; 72 73 76 public DigitValidator() { 77 this(null); 78 } 79 80 85 public DigitValidator(String ierrmsg) { 86 super(ierrmsg); 87 } 88 89 96 public DigitValidator(int ilen) { 97 this(ilen, null); 99 requireLength = true; 100 } 101 102 108 public DigitValidator(int ilen, String ierrmsg) { 109 super(ierrmsg); 111 len = ilen; 112 requireLength = true; 113 } 114 115 124 public DigitValidator(int ilen, boolean idomesticPhone, String ierrmsg) { 125 super(ierrmsg); 126 len = ilen; 127 domesticPhone = idomesticPhone; 128 phoneNumber = true; 129 requireLength = true; 130 } 131 132 138 public boolean isDomesticPhone() { 139 return domesticPhone; 140 } 141 142 154 public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException { 155 162 if (this.isNull(val, element)) 165 return; 166 168 String s = null; 171 char c[] = null; 172 s = val.toString().trim(); 174 c = s.toCharArray(); 175 178 if ( element.getParseException()!=null) { 187 throw this.generateException(element, 188 deferExceptions, 189 s + " contains characters other than digits because " + element.getParseException()); 190 191 } 192 195 if (!requireLength) { 199 if (s!=null && s.length()>0) { 200 if (localLogger.isInfoEnabled()) localLogger.info("validating to see if val {"+s+"} contains only the numbers 0-9"); 202 for (int i=0; i<c.length; i++) { 203 if ((c[i] < '0') || (c[i] > '9')) { 204 throw this.generateException(element, deferExceptions, "Val {"+val+"} contains characters other than 0123456789"); 205 } 206 } 207 } 208 209 } else { 213 if (s==null || s.length()<1) { 215 throw this.generateException(element, deferExceptions, "Null value fails to meet minimum length of "+len); 216 } 217 218 if (localLogger.isInfoEnabled()) localLogger.info("validating to see if length of val {"+s+"} = "+len); 220 if (s.length()!=len) { 221 throw this.generateException(element, deferExceptions, "Length of val {"+val+"} is not equal to the field length of "+len); 222 } 223 224 if (localLogger.isInfoEnabled()) localLogger.info("validating to see if val {"+s+"} contains only the numbers 0-9"); 226 for (int i=0; i<c.length; i++) { 227 if ((c[i] < '0') || (c[i] > '9')) { 228 throw this.generateException(element, deferExceptions, "Val {"+val+"} contains characters other than 0123456789"); 229 } 230 } 231 232 if (phoneNumber) { 234 if (domesticPhone) { 236 if (localLogger.isInfoEnabled()) localLogger.info("Validating to see if val {"+s+"} for domestic phone requirments"); 237 if ((c.length==3) && (c[0]=='0')) { 238 throw this.generateException(element, deferExceptions, "Val {"+val+"} starts with a zero for a length of 3"); 239 } 240 } else { 242 if (localLogger.isInfoEnabled()) localLogger.info("Validating val {"+s+"} for international phone requirments"); 243 } 245 } 246 } 247 } 248 } 249 | Popular Tags |