KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilValidate


1 /*
2  * $Id: UtilValidate.java 5578 2005-08-23 13:59:58Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.util.Calendar JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 /**
30  * General input/data validation methods
31  * Utility methods for validating data, especially input.
32  * See detailed description below.
33  *
34  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
35  * @version $Rev: 5578 $
36  * @since 1.0
37  *
38  *
39  * <br/> SUMMARY
40  * <br/>
41  * <br/> This is a set of meethods for validating input. Functions are provided to validate:
42  * <br/> - U.S. and international phone/fax numbers
43  * <br/> - U.S. ZIP codes(5 or 9 digit postal codes)
44  * <br/> - U.S. Postal Codes(2 letter abbreviations for names of states)
45  * <br/> - U.S. Social Security Numbers(abbreviated as SSNs)
46  * <br/> - email addresses
47  * <br/> - dates(entry of year, month, and day and validity of combined date)
48  * <br/> - credit card numbers
49  * <br/>
50  * <br/> Supporting utility functions validate that:
51  * <br/> - characters are Letter, Digit, or LetterOrDigit
52  * <br/> - strings are a Signed, Positive, Negative, Nonpositive, or Nonnegative integer
53  * <br/> - strings are a Float or a SignedFloat
54  * <br/> - strings are Alphabetic, Alphanumeric, or Whitespace
55  * <br/> - strings contain an integer within a specified range
56  * <br/>
57  * <br/> Other utility functions are provided to:
58  * <br/> - remove from a string characters which are/are not in a "bag" of selected characters
59  * <br/> - strip whitespace/leading whitespace from a string
60  * <br/>
61  * <br/> ==============================================================================
62  * <br/> NOTE: This code was adapted from the Netscape JavaScript form validation code,
63  * <br/> usually found in "FormChek.js". Credit card verification functions Originally
64  * <br< included as Starter Application 1.0.0 in LivePayment.
65  * <br/> ==============================================================================
66  */

67 public class UtilValidate {
68
69     public static final String JavaDoc module = UtilValidate.class.getName();
70
71     /** boolean specifying by default whether or not it is okay for a String to be empty */
72     public static final boolean defaultEmptyOK = true;
73
74     /** digit characters */
75     public static final String JavaDoc digits = "0123456789";
76
77     /** lower-case letter characters */
78     public static final String JavaDoc lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
79
80     /** upper-case letter characters */
81     public static final String JavaDoc uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
82
83     /** letter characters */
84     public static final String JavaDoc letters = lowercaseLetters + uppercaseLetters;
85
86     /** whitespace characters */
87     public static final String JavaDoc whitespace = " \t\n\r";
88
89     /** decimal point character differs by language and culture */
90     public static final String JavaDoc decimalPointDelimiter = ".";
91
92     /** non-digit characters which are allowed in phone numbers */
93     public static final String JavaDoc phoneNumberDelimiters = "()- ";
94
95     /** characters which are allowed in US phone numbers */
96     public static final String JavaDoc validUSPhoneChars = digits + phoneNumberDelimiters;
97
98     /** characters which are allowed in international phone numbers(a leading + is OK) */
99     public static final String JavaDoc validWorldPhoneChars = digits + phoneNumberDelimiters + "+";
100
101     /** non-digit characters which are allowed in Social Security Numbers */
102     public static final String JavaDoc SSNDelimiters = "- ";
103
104     /** characters which are allowed in Social Security Numbers */
105     public static final String JavaDoc validSSNChars = digits + SSNDelimiters;
106
107     /** U.S. Social Security Numbers have 9 digits. They are formatted as 123-45-6789. */
108     public static final int digitsInSocialSecurityNumber = 9;
109
110     /** U.S. phone numbers have 10 digits. They are formatted as 123 456 7890 or(123) 456-7890. */
111     public static final int digitsInUSPhoneNumber = 10;
112     public static final int digitsInUSPhoneAreaCode = 3;
113     public static final int digitsInUSPhoneMainNumber = 7;
114
115     /** non-digit characters which are allowed in ZIP Codes */
116     public static final String JavaDoc ZipCodeDelimiters = "-";
117
118     /** our preferred delimiter for reformatting ZIP Codes */
119     public static final String JavaDoc ZipCodeDelimeter = "-";
120
121     /** characters which are allowed in Social Security Numbers */
122     public static final String JavaDoc validZipCodeChars = digits + ZipCodeDelimiters;
123
124     /** U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789. */
125     public static final int digitsInZipCode1 = 5;
126
127     /** U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789. */
128     public static final int digitsInZipCode2 = 9;
129
130     /** non-digit characters which are allowed in credit card numbers */
131     public static final String JavaDoc creditCardDelimiters = " -";
132
133     public static final String JavaDoc isNotEmptyMsg = "This field cannot be empty, please enter a value.";
134     public static final String JavaDoc isStateCodeMsg = "The State Code must be a valid two character U.S. state abbreviation(like CA for California).";
135     public static final String JavaDoc isContiguousStateCodeMsg = "The State Code must be a valid two character U.S. state abbreviation for one of the 48 contiguous United States (like CA for California).";
136     public static final String JavaDoc isZipCodeMsg = "The ZIP Code must be a 5 or 9 digit U.S. ZIP Code(like 94043).";
137     public static final String JavaDoc isUSPhoneMsg = "The US Phone must be a 10 digit U.S. phone number(like 415-555-1212).";
138     public static final String JavaDoc isUSPhoneAreaCodeMsg = "The Phone Number Area Code must be 3 digits.";
139     public static final String JavaDoc isUSPhoneMainNumberMsg = "The Phone Number must be 7 digits.";
140     public static final String JavaDoc isContiguousZipCodeMsg = "Zip Code is not a valid Zip Code for one of the 48 contiguous United States .";
141     public static final String JavaDoc isInternationalPhoneNumberMsg = "The World Phone must be a valid international phone number.";
142     public static final String JavaDoc isSSNMsg = "The SSN must be a 9 digit U.S. social security number(like 123-45-6789).";
143     public static final String JavaDoc isEmailMsg = "The Email must be a valid email address(like john@email.com). Please re-enter it now.";
144     public static final String JavaDoc isAnyCardMsg = "The credit card number is not a valid card number.";
145     public static final String JavaDoc isCreditCardPrefixMsg = " is not a valid ";
146     public static final String JavaDoc isCreditCardSuffixMsg = " credit card number.";
147     public static final String JavaDoc isDayMsg = "The Day must be a day number between 1 and 31. ";
148     public static final String JavaDoc isMonthMsg = "The Month must be a month number between 1 and 12. ";
149     public static final String JavaDoc isYearMsg = "The Year must be a 2 or 4 digit year number. ";
150     public static final String JavaDoc isDatePrefixMsg = "The Day, Month, and Year for ";
151     public static final String JavaDoc isDateSuffixMsg = " do not form a valid date. Please reenter them now.";
152     public static final String JavaDoc isHourMsg = "The Hour must be a number between 0 and 23.";
153     public static final String JavaDoc isMinuteMsg = "The Hour must be a number between 0 and 59.";
154     public static final String JavaDoc isSecondMsg = "The Hour must be a number between 0 and 59.";
155     public static final String JavaDoc isTimeMsg = "The Time must be a valid time formed like: HH:MM or HH:MM:SS.";
156     public static final String JavaDoc isDateMsg = "The Date must be a valid date formed like: MM/YY, MM/YYYY, MM/DD/YY, or MM/DD/YYYY.";
157     public static final String JavaDoc isDateAfterToday = "The Date must be a valid date after today, and formed like: MM/YY, MM/YYYY, MM/DD/YY, or MM/DD/YYYY.";
158     public static final String JavaDoc isIntegerMsg = "The Number must be a valid unsigned whole decimal number.";
159     public static final String JavaDoc isSignedIntegerMsg = "The Number must be a valid signed whole decimal number.";
160     public static final String JavaDoc isLongMsg = "The Number must be a valid unsigned whole decimal number.";
161     public static final String JavaDoc isSignedLongMsg = "The Number must be a valid signed whole decimal number.";
162     public static final String JavaDoc isFloatMsg = "The Number must be a valid unsigned decimal number.";
163     public static final String JavaDoc isSignedFloatMsg = "The Number must be a valid signed decimal number.";
164     public static final String JavaDoc isSignedDoubleMsg = "The Number must be a valid signed decimal number.";
165
166     /** An array of ints representing the number of days in each month of the year.
167      * Note: February varies depending on the year */

168     public static final int[] daysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
169
170     /** Delimiter for USStateCodes String */
171     public static final String JavaDoc USStateCodeDelimiter = "|";
172
173     /** Valid U.S. Postal Codes for states, territories, armed forces, etc.
174      * See http://www.usps.gov/ncsc/lookups/abbr_state.txt. */

175     public static final String JavaDoc USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";
176
177     /** Valid contiguous U.S. postal codes */
178     public static final String JavaDoc ContiguousUSStateCodes = "AL|AZ|AR|CA|CO|CT|DE|DC|FL|GA|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY";
179
180     public static boolean areEqual(Object JavaDoc obj, Object JavaDoc obj2) {
181         if (obj == null) {
182             return obj2 == null;
183         } else {
184             return obj.equals(obj2);
185         }
186     }
187
188     /** Check whether an object is empty, will see if it is a String, Map, Collection, etc. */
189     public static boolean isEmpty(Object JavaDoc o) {
190         return ObjectType.isEmpty(o);
191     }
192
193     /** Check whether string s is empty. */
194     public static boolean isEmpty(String JavaDoc s) {
195         return ((s == null) || (s.length() == 0));
196     }
197
198     /** Check whether collection c is empty. */
199     public static boolean isEmpty(Collection JavaDoc c) {
200         return ((c == null) || (c.size() == 0));
201     }
202
203     /** Check whether string s is NOT empty. */
204     public static boolean isNotEmpty(String JavaDoc s) {
205         return ((s != null) && (s.length() > 0));
206     }
207
208     /** Check whether collection c is NOT empty. */
209     public static boolean isNotEmpty(Collection JavaDoc c) {
210         return ((c != null) && (c.size() > 0));
211     }
212
213     public static boolean isString(Object JavaDoc obj) {
214         return ((obj != null) && (obj instanceof java.lang.String JavaDoc));
215     }
216
217     /** Returns true if string s is empty or whitespace characters only. */
218     public static boolean isWhitespace(String JavaDoc s) {
219         // Is s empty?
220
if (isEmpty(s)) return true;
221
222         // Search through string's characters one by one
223
// until we find a non-whitespace character.
224
// When we do, return false; if we don't, return true.
225
for (int i = 0; i < s.length(); i++) {
226             // Check that current character isn't whitespace.
227
char c = s.charAt(i);
228
229             if (whitespace.indexOf(c) == -1) return false;
230         }
231         // All characters are whitespace.
232
return true;
233     }
234
235     /** Removes all characters which appear in string bag from string s. */
236     public static String JavaDoc stripCharsInBag(String JavaDoc s, String JavaDoc bag) {
237         int i;
238         String JavaDoc returnString = "";
239
240         // Search through string's characters one by one.
241
// If character is not in bag, append to returnString.
242
for (i = 0; i < s.length(); i++) {
243             char c = s.charAt(i);
244
245             if (bag.indexOf(c) == -1) returnString += c;
246         }
247         return returnString;
248     }
249
250     /** Removes all characters which do NOT appear in string bag from string s. */
251     public static String JavaDoc stripCharsNotInBag(String JavaDoc s, String JavaDoc bag) {
252         int i;
253         String JavaDoc returnString = "";
254
255         // Search through string's characters one by one.
256
// If character is in bag, append to returnString.
257
for (i = 0; i < s.length(); i++) {
258             char c = s.charAt(i);
259
260             if (bag.indexOf(c) != -1) returnString += c;
261         }
262         return returnString;
263     }
264
265     /** Removes all whitespace characters from s.
266      * Member whitespace(see above) defines which characters are considered whitespace. */

267     public static String JavaDoc stripWhitespace(String JavaDoc s) {
268         return stripCharsInBag(s, whitespace);
269     }
270
271     /** Returns true if single character c(actually a string) is contained within string s. */
272     public static boolean charInString(char c, String JavaDoc s) {
273         return (s.indexOf(c) != -1);
274         // for(int i = 0; i < s.length; i++) {
275
// if(s.charAt(i) == c) return true;
276
// }
277
// return false;
278
}
279
280     /** Removes initial(leading) whitespace characters from s.
281      * Member whitespace(see above) defines which characters are considered whitespace. */

282     public static String JavaDoc stripInitialWhitespace(String JavaDoc s) {
283         int i = 0;
284
285         while ((i < s.length()) && charInString(s.charAt(i), whitespace)) i++;
286         return s.substring(i);
287         // return s.substring(i, s.length);
288
}
289
290     /** Returns true if character c is an English letter (A .. Z, a..z).
291      *
292      * NOTE: Need i18n version to support European characters.
293      * This could be tricky due to different character
294      * sets and orderings for various languages and platforms. */

295     public static boolean isLetter(char c) {
296         return Character.isLetter(c);
297     }
298
299     /** Returns true if character c is a digit (0 .. 9). */
300     public static boolean isDigit(char c) {
301         return Character.isDigit(c);
302     }
303
304     /** Returns true if character c is a letter or digit. */
305     public static boolean isLetterOrDigit(char c) {
306         return Character.isLetterOrDigit(c);
307     }
308
309     /** Returns true if all characters in string s are numbers.
310      *
311      * Accepts non-signed integers only. Does not accept floating
312      * point, exponential notation, etc.
313      */

314     public static boolean isInteger(String JavaDoc s) {
315         if (isEmpty(s)) return defaultEmptyOK;
316
317         // Search through string's characters one by one
318
// until we find a non-numeric character.
319
// When we do, return false; if we don't, return true.
320
for (int i = 0; i < s.length(); i++) {
321             // Check that current character is number.
322
char c = s.charAt(i);
323
324             if (!isDigit(c)) return false;
325         }
326
327         // All characters are numbers.
328
return true;
329     }
330
331     /** Returns true if all characters are numbers;
332      * first character is allowed to be + or - as well.
333      *
334      * Does not accept floating point, exponential notation, etc.
335      */

336     public static boolean isSignedInteger(String JavaDoc s) {
337         if (isEmpty(s)) return defaultEmptyOK;
338         try {
339             Integer.parseInt(s);
340             return true;
341         } catch (Exception JavaDoc e) {
342             return false;
343         }
344
345         // int startPos = 0;
346
// boolean secondArg = defaultEmptyOK;
347

348         // if(isSignedInteger.arguments.length > 1) secondArg = isSignedInteger.arguments[1];
349

350         // skip leading + or -
351
// if((s.charAt(0) == "-") ||(s.charAt(0) == "+") ) startPos = 1;
352
// return(isInteger(s.substring(startPos, s.length), secondArg))
353
}
354
355     /** Returns true if all characters are numbers;
356      * first character is allowed to be + or - as well.
357      *
358      * Does not accept floating point, exponential notation, etc.
359      */

360     public static boolean isSignedLong(String JavaDoc s) {
361         if (isEmpty(s)) return defaultEmptyOK;
362         try {
363             Long.parseLong(s);
364             return true;
365         } catch (Exception JavaDoc e) {
366             return false;
367         }
368     }
369
370     /** Returns true if string s is an integer > 0. NOTE: using the Java Long object for greatest precision */
371     public static boolean isPositiveInteger(String JavaDoc s) {
372         if (isEmpty(s)) return defaultEmptyOK;
373
374         try {
375             long temp = Long.parseLong(s);
376
377             if (temp > 0) return true;
378             return false;
379         } catch (Exception JavaDoc e) {
380             return false;
381         }
382
383         // return(isSignedInteger(s, secondArg)
384
// &&((isEmpty(s) && secondArg) ||(parseInt(s) > 0) ) );
385
}
386
387     /** Returns true if string s is an integer >= 0. */
388     public static boolean isNonnegativeInteger(String JavaDoc s) {
389         if (isEmpty(s)) return defaultEmptyOK;
390
391         try {
392             int temp = Integer.parseInt(s);
393
394             if (temp >= 0) return true;
395             return false;
396         } catch (Exception JavaDoc e) {
397             return false;
398         }
399
400         // return(isSignedInteger(s, secondArg)
401
// &&((isEmpty(s) && secondArg) ||(parseInt(s) >= 0) ) );
402
}
403
404     /** Returns true if string s is an integer < 0. */
405     public static boolean isNegativeInteger(String JavaDoc s) {
406         if (isEmpty(s)) return defaultEmptyOK;
407
408         try {
409             int temp = Integer.parseInt(s);
410
411             if (temp < 0) return true;
412             return false;
413         } catch (Exception JavaDoc e) {
414             return false;
415         }
416
417         // return(isSignedInteger(s, secondArg)
418
// &&((isEmpty(s) && secondArg) ||(parseInt(s) < 0) ) );
419
}
420
421     /** Returns true if string s is an integer <= 0. */
422     public static boolean isNonpositiveInteger(String JavaDoc s) {
423         if (isEmpty(s)) return defaultEmptyOK;
424
425         try {
426             int temp = Integer.parseInt(s);
427
428             if (temp <= 0) return true;
429             return false;
430         } catch (Exception JavaDoc e) {
431             return false;
432         }
433
434         // return(isSignedInteger(s, secondArg)
435
// &&((isEmpty(s) && secondArg) ||(parseInt(s) <= 0) ) );
436
}
437
438     /** True if string s is an unsigned floating point(real) number.
439      *
440      * Also returns true for unsigned integers. If you wish
441      * to distinguish between integers and floating point numbers,
442      * first call isInteger, then call isFloat.
443      *
444      * Does not accept exponential notation.
445      */

446     public static boolean isFloat(String JavaDoc s) {
447         if (isEmpty(s)) return defaultEmptyOK;
448
449         boolean seenDecimalPoint = false;
450
451         if (s.startsWith(decimalPointDelimiter)) return false;
452
453         // Search through string's characters one by one
454
// until we find a non-numeric character.
455
// When we do, return false; if we don't, return true.
456
for (int i = 0; i < s.length(); i++) {
457             // Check that current character is number.
458
char c = s.charAt(i);
459
460             if (c == decimalPointDelimiter.charAt(0)) {
461                 if (!seenDecimalPoint) {
462                     seenDecimalPoint = true;
463                 } else {
464                     return false;
465                 }
466             } else {
467                 if (!isDigit(c)) return false;
468             }
469         }
470         // All characters are numbers.
471
return true;
472     }
473
474     /** General routine for testing whether a string is a float.
475      */

476     public static boolean isFloat(String JavaDoc s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal) {
477         if (isEmpty(s)) return defaultEmptyOK;
478
479         try {
480             float temp = Float.parseFloat(s);
481             if (!allowNegative && temp < 0) return false;
482             if (!allowPositive && temp > 0) return false;
483             String JavaDoc floatString = Float.toString(temp);
484             int decimalPoint = floatString.indexOf(".");
485             if (decimalPoint == -1) {
486                 if (minDecimal > 0) return false;
487                 return true;
488             }
489             // 1.2345; length=6; point=1; num=4
490
int numDecimals = floatString.length() - decimalPoint;
491             if (minDecimal >= 0 && numDecimals < minDecimal) return false;
492             if (maxDecimal >= 0 && numDecimals > maxDecimal) return false;
493             return true;
494         } catch (Exception JavaDoc e) {
495             return false;
496         }
497     }
498
499     /** General routine for testing whether a string is a double.
500      */

501     public static boolean isDouble(String JavaDoc s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal) {
502         if (isEmpty(s)) return defaultEmptyOK;
503
504         try {
505             double temp = Double.parseDouble(s);
506             if (!allowNegative && temp < 0) return false;
507             if (!allowPositive && temp > 0) return false;
508             String JavaDoc doubleString = Double.toString(temp);
509             int decimalPoint = doubleString.indexOf(".");
510             if (decimalPoint == -1) {
511                 if (minDecimal > 0) return false;
512                 return true;
513             }
514             // 1.2345; length=6; point=1; num=4
515
int numDecimals = doubleString.length() - decimalPoint;
516             if (minDecimal >= 0 && numDecimals < minDecimal) return false;
517             if (maxDecimal >= 0 && numDecimals > maxDecimal) return false;
518             return true;
519         } catch (Exception JavaDoc e) {
520             return false;
521         }
522     }
523
524     /** True if string s is a signed or unsigned floating point
525      * (real) number. First character is allowed to be + or -.
526      *
527      * Also returns true for unsigned integers. If you wish
528      * to distinguish between integers and floating point numbers,
529      * first call isSignedInteger, then call isSignedFloat.
530      */

531     public static boolean isSignedFloat(String JavaDoc s) {
532         if (isEmpty(s)) return defaultEmptyOK;
533         try {
534             Float.parseFloat(s);
535             return true;
536         } catch (Exception JavaDoc e) {
537             return false;
538         }
539
540         //The old way:
541
// int startPos = 0;
542
// if(isSignedFloat.arguments.length > 1) secondArg = isSignedFloat.arguments[1];
543
// skip leading + or -
544
// if((s.charAt(0) == "-") ||(s.charAt(0) == "+") ) startPos = 1;
545
// return(isFloat(s.substring(startPos, s.length), secondArg))
546
}
547
548     /** True if string s is a signed or unsigned floating point
549      * (real) number. First character is allowed to be + or -.
550      *
551      * Also returns true for unsigned integers. If you wish
552      * to distinguish between integers and floating point numbers,
553      * first call isSignedInteger, then call isSignedFloat.
554      */

555     public static boolean isSignedDouble(String JavaDoc s) {
556         if (isEmpty(s)) return defaultEmptyOK;
557         try {
558             Double.parseDouble(s);
559             return true;
560         } catch (Exception JavaDoc e) {
561             return false;
562         }
563     }
564
565     /** Returns true if string s is letters only.
566      *
567      * NOTE: This should handle i18n version to support European characters, etc.
568      * since it now uses Character.isLetter()
569      */

570     public static boolean isAlphabetic(String JavaDoc s) {
571         if (isEmpty(s)) return defaultEmptyOK;
572
573         // Search through string's characters one by one
574
// until we find a non-alphabetic character.
575
// When we do, return false; if we don't, return true.
576
for (int i = 0; i < s.length(); i++) {
577             // Check that current character is letter.
578
char c = s.charAt(i);
579
580             if (!isLetter(c))
581                 return false;
582         }
583
584         // All characters are letters.
585
return true;
586     }
587
588     /** Returns true if string s is English letters (A .. Z, a..z) and numbers only.
589      *
590      * NOTE: Need i18n version to support European characters.
591      * This could be tricky due to different character
592      * sets and orderings for various languages and platforms.
593      */

594     public static boolean isAlphanumeric(String JavaDoc s) {
595         if (isEmpty(s)) return defaultEmptyOK;
596
597         // Search through string's characters one by one
598
// until we find a non-alphanumeric character.
599
// When we do, return false; if we don't, return true.
600
for (int i = 0; i < s.length(); i++) {
601             // Check that current character is number or letter.
602
char c = s.charAt(i);
603
604             if (!isLetterOrDigit(c)) return false;
605         }
606
607         // All characters are numbers or letters.
608
return true;
609     }
610
611     /* ================== METHODS TO CHECK VARIOUS FIELDS. ==================== */
612
613     /** isSSN returns true if string s is a valid U.S. Social Security Number. Must be 9 digits. */
614     public static boolean isSSN(String JavaDoc s) {
615         if (isEmpty(s)) return defaultEmptyOK;
616
617         String JavaDoc normalizedSSN = stripCharsInBag(s, SSNDelimiters);
618
619         return (isInteger(normalizedSSN) && normalizedSSN.length() == digitsInSocialSecurityNumber);
620     }
621
622     /** isUSPhoneNumber returns true if string s is a valid U.S. Phone Number. Must be 10 digits. */
623     public static boolean isUSPhoneNumber(String JavaDoc s) {
624         if (isEmpty(s)) return defaultEmptyOK;
625         String JavaDoc normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);
626
627         return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneNumber);
628     }
629
630     /** isUSPhoneAreaCode returns true if string s is a valid U.S. Phone Area Code. Must be 3 digits. */
631     public static boolean isUSPhoneAreaCode(String JavaDoc s) {
632         if (isEmpty(s)) return defaultEmptyOK;
633         String JavaDoc normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);
634
635         return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneAreaCode);
636     }
637
638     /** isUSPhoneMainNumber returns true if string s is a valid U.S. Phone Main Number. Must be 7 digits. */
639     public static boolean isUSPhoneMainNumber(String JavaDoc s) {
640         if (isEmpty(s)) return defaultEmptyOK;
641         String JavaDoc normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);
642
643         return (isInteger(normalizedPhone) && normalizedPhone.length() == digitsInUSPhoneMainNumber);
644     }
645
646     /** isInternationalPhoneNumber returns true if string s is a valid
647      * international phone number. Must be digits only; any length OK.
648      * May be prefixed by + character.
649      */

650     public static boolean isInternationalPhoneNumber(String JavaDoc s) {
651         if (isEmpty(s)) return defaultEmptyOK;
652
653         String JavaDoc normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);
654
655         return isPositiveInteger(normalizedPhone);
656     }
657
658     /** isZIPCode returns true if string s is a valid U.S. ZIP code. Must be 5 or 9 digits only. */
659     public static boolean isZipCode(String JavaDoc s) {
660         if (isEmpty(s)) return defaultEmptyOK;
661
662         String JavaDoc normalizedZip = stripCharsInBag(s, ZipCodeDelimiters);
663
664         return (isInteger(normalizedZip) && ((normalizedZip.length() == digitsInZipCode1) || (normalizedZip.length() == digitsInZipCode2)));
665     }
666
667     /** Returns true if string s is a valid contiguous U.S. Zip code. Must be 5 or 9 digits only. */
668     public static boolean isContiguousZipCode(String JavaDoc s) {
669         boolean retval = false;
670         if (isZipCode(s)) {
671             if (isEmpty(s)) retval = defaultEmptyOK;
672             else {
673                 String JavaDoc normalizedZip = s.substring(0,5);
674                 int iZip = Integer.parseInt(normalizedZip);
675                 if ((iZip >= 96701 && iZip <= 96898) || (iZip >= 99501 && iZip <= 99950)) retval = false;
676                 else retval = true;
677             }
678         }
679         return retval;
680     }
681
682     /** Return true if s is a valid U.S. Postal Code (abbreviation for state). */
683     public static boolean isStateCode(String JavaDoc s) {
684         if (isEmpty(s)) return defaultEmptyOK;
685         return ((USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1));
686     }
687
688     /** Return true if s is a valid contiguous U.S. Postal Code (abbreviation for state). */
689     public static boolean isContiguousStateCode(String JavaDoc s) {
690         if (isEmpty(s)) return defaultEmptyOK;
691         return ((ContiguousUSStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1));
692     }
693
694     public static boolean isEmail(String JavaDoc s) {
695         return isEmail(s, false);
696     }
697
698     /** Email address must be of form a@b.c -- in other words:
699      * - there must be at least one character before the @
700      * - there must be at least one character before and after the .
701      * - the character @ is required, and . requirement is controlled
702      * - by requireDot
703      */

704     public static boolean isEmail(String JavaDoc s, boolean requireDot) {
705
706         // todo: use regular expression validation
707

708         if (isEmpty(s)) return defaultEmptyOK;
709
710         // is s whitespace?
711
if (isWhitespace(s)) return false;
712
713         int atSymbolIndex = s.indexOf('@');
714
715         // there must be >= 1 character before @
716
// indexOf returns -1 if char not found, so 0 or -1 are bad
717
if (atSymbolIndex <= 0 ) return false;
718
719         if (requireDot) {
720             int dotIndex = s.lastIndexOf('.');
721             if (dotIndex == -1) return false; // no dot
722
if (dotIndex < atSymbolIndex + 2) return false; // nothing between @ and .
723
if (dotIndex == s.length() - 1 ) return false; // . is last character
724
}
725
726         return true;
727     }
728
729     /** isUrl returns true if the string contains ://
730      * @param s String to validate
731      * @return true if s contains ://
732      */

733     public static boolean isUrl(String JavaDoc s) {
734         if (isEmpty(s)) return defaultEmptyOK;
735         if (s.indexOf("://") != -1)
736             return true;
737         return false;
738     }
739
740     /** isYear returns true if string s is a valid
741      * Year number. Must be 2 or 4 digits only.
742      *
743      * For Year 2000 compliance, you are advised
744      * to use 4-digit year numbers everywhere.
745      */

746     public static boolean isYear(String JavaDoc s) {
747         if (isEmpty(s)) return defaultEmptyOK;
748
749         if (!isNonnegativeInteger(s)) return false;
750         return ((s.length() == 2) || (s.length() == 4));
751     }
752
753     /** isIntegerInRange returns true if string s is an integer
754      * within the range of integer arguments a and b, inclusive.
755      */

756     public static boolean isIntegerInRange(String JavaDoc s, int a, int b) {
757         if (isEmpty(s)) return defaultEmptyOK;
758         // Catch non-integer strings to avoid creating a NaN below,
759
// which isn't available on JavaScript 1.0 for Windows.
760
if (!isSignedInteger(s)) return false;
761         // Now, explicitly change the type to integer via parseInt
762
// so that the comparison code below will work both on
763
// JavaScript 1.2(which typechecks in equality comparisons)
764
// and JavaScript 1.1 and before(which doesn't).
765
int num = Integer.parseInt(s);
766
767         return ((num >= a) && (num <= b));
768     }
769
770     /** isMonth returns true if string s is a valid month number between 1 and 12. */
771     public static boolean isMonth(String JavaDoc s) {
772         if (isEmpty(s)) return defaultEmptyOK;
773         return isIntegerInRange(s, 1, 12);
774     }
775
776     /** isDay returns true if string s is a valid day number between 1 and 31. */
777     public static boolean isDay(String JavaDoc s) {
778         if (isEmpty(s)) return defaultEmptyOK;
779         return isIntegerInRange(s, 1, 31);
780     }
781
782     /** Given integer argument year, returns number of days in February of that year. */
783     public static int daysInFebruary(int year) {
784         // February has 29 days in any year evenly divisible by four,
785
// EXCEPT for centurial years which are not also divisible by 400.
786
return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
787     }
788
789     /** isHour returns true if string s is a valid number between 0 and 23. */
790     public static boolean isHour(String JavaDoc s) {
791         if (isEmpty(s)) return defaultEmptyOK;
792         return isIntegerInRange(s, 0, 23);
793     }
794
795     /** isMinute returns true if string s is a valid number between 0 and 59. */
796     public static boolean isMinute(String JavaDoc s) {
797         if (isEmpty(s)) return defaultEmptyOK;
798         return isIntegerInRange(s, 0, 59);
799     }
800
801     /** isSecond returns true if string s is a valid number between 0 and 59. */
802     public static boolean isSecond(String JavaDoc s) {
803         if (isEmpty(s)) return defaultEmptyOK;
804         return isIntegerInRange(s, 0, 59);
805     }
806
807     /** isDate returns true if string arguments year, month, and day form a valid date. */
808     public static boolean isDate(String JavaDoc year, String JavaDoc month, String JavaDoc day) {
809         // catch invalid years(not 2- or 4-digit) and invalid months and days.
810
if (!(isYear(year) && isMonth(month) && isDay(day))) return false;
811
812         int intYear = Integer.parseInt(year);
813         int intMonth = Integer.parseInt(month);
814         int intDay = Integer.parseInt(day);
815
816         // catch invalid days, except for February
817
if (intDay > daysInMonth[intMonth - 1]) return false;
818         if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
819         return true;
820     }
821
822     /** isDate returns true if string argument date forms a valid date. */
823     public static boolean isDate(String JavaDoc date) {
824         if (isEmpty(date)) return defaultEmptyOK;
825         String JavaDoc month;
826         String JavaDoc day;
827         String JavaDoc year;
828
829         int dateSlash1 = date.indexOf("/");
830         int dateSlash2 = date.lastIndexOf("/");
831
832         if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return false;
833         month = date.substring(0, dateSlash1);
834         day = date.substring(dateSlash1 + 1, dateSlash2);
835         year = date.substring(dateSlash2 + 1);
836
837         return isDate(year, month, day);
838     }
839
840     /** isDate returns true if string argument date forms a valid date and is after today. */
841     public static boolean isDateAfterToday(String JavaDoc date) {
842         if (isEmpty(date)) return defaultEmptyOK;
843         int dateSlash1 = date.indexOf("/");
844         int dateSlash2 = date.lastIndexOf("/");
845
846         if (dateSlash1 <= 0) return false;
847
848         java.util.Date JavaDoc passed = null;
849         if (dateSlash1 == dateSlash2) {
850             // consider the day to be optional; use the first day of the following month for comparison since this is an is after test
851
String JavaDoc month = date.substring(0, dateSlash1);
852             String JavaDoc day = "28";
853             String JavaDoc year = date.substring(dateSlash1 + 1);
854             if (!isDate(year, month, day)) return false;
855
856             try {
857                 int monthInt = Integer.parseInt(month);
858                 int yearInt = Integer.parseInt(year);
859                 Calendar JavaDoc calendar = Calendar.getInstance();
860                 calendar.set(yearInt, monthInt - 1, 0, 0, 0, 0);
861                 calendar.add(Calendar.MONTH, 1);
862                 passed = new java.util.Date JavaDoc(calendar.getTime().getTime());
863             } catch (Exception JavaDoc e) {
864                 passed = null;
865             }
866         } else {
867             String JavaDoc month = date.substring(0, dateSlash1);
868             String JavaDoc day = date.substring(dateSlash1 + 1, dateSlash2);
869             String JavaDoc year = date.substring(dateSlash2 + 1);
870             if (!isDate(year, month, day)) return false;
871             passed = UtilDateTime.toDate(month, day, year, "0", "0", "0");
872         }
873
874         java.util.Date JavaDoc now = UtilDateTime.nowDate();
875         if (passed != null) {
876             return passed.after(now);
877         } else {
878             return false;
879         }
880     }
881
882     /** isTime returns true if string arguments hour, minute, and second form a valid time. */
883     public static boolean isTime(String JavaDoc hour, String JavaDoc minute, String JavaDoc second) {
884         // catch invalid years(not 2- or 4-digit) and invalid months and days.
885
if (isHour(hour) && isMinute(minute) && isSecond(second))
886             return true;
887         else
888             return false;
889     }
890
891     /** isTime returns true if string argument time forms a valid time. */
892     public static boolean isTime(String JavaDoc time) {
893         if (isEmpty(time)) return defaultEmptyOK;
894
895         String JavaDoc hour;
896         String JavaDoc minute;
897         String JavaDoc second;
898
899         int timeColon1 = time.indexOf(":");
900         int timeColon2 = time.lastIndexOf(":");
901
902         if (timeColon1 <= 0) return false;
903         hour = time.substring(0, timeColon1);
904         if (timeColon1 == timeColon2) {
905             minute = time.substring(timeColon1 + 1);
906             second = "0";
907         } else {
908             minute = time.substring(timeColon1 + 1, timeColon2);
909             second = time.substring(timeColon2 + 1);
910         }
911         return isTime(hour, minute, second);
912     }
913
914     /** Check to see if a card number is a valid ValueLink Gift Card
915      *
916      * @param stPassed a string representing a valuelink gift card
917      * @return true, if the number passed simple checks
918      */

919     public static boolean isValueLinkCard(String JavaDoc stPassed) {
920         if (isEmpty(stPassed)) return defaultEmptyOK;
921         String JavaDoc st = stripCharsInBag(stPassed, creditCardDelimiters);
922         if (st.length() == 16 && (st.startsWith("7") || st.startsWith("6"))) {
923             return true;
924         }
925         return false;
926     }
927
928     /** Check to see if a card number is a valid OFB Gift Card (Certifiicate)
929      *
930      * @param stPassed a string representing a gift card
931      * @return tru, if the number passed simple checks
932      */

933     public static boolean isOFBGiftCard(String JavaDoc stPassed) {
934         if (isEmpty(stPassed)) return defaultEmptyOK;
935         String JavaDoc st = stripCharsInBag(stPassed, creditCardDelimiters);
936         if (st.length() == 15 && sumIsMod10(getLuhnSum(st))) {
937             return true;
938         }
939         return false;
940     }
941
942     /** Check to see if a card number is a supported Gift Card
943      *
944      * @param stPassed a string representing a gift card
945      * @return true, if the number passed simple checks
946      */

947     public static boolean isGiftCard(String JavaDoc stPassed) {
948         if (isOFBGiftCard(stPassed)) {
949             return true;
950         } else if (isValueLinkCard(stPassed)) {
951             return true;
952         }
953         return false;
954     }
955
956     public static int getLuhnSum(String JavaDoc stPassed) {
957         stPassed = stPassed.replaceAll("\\D", ""); // nuke any non-digit characters
958

959         int len = stPassed.length();
960         int sum = 0;
961         int mul = 1;
962         for (int i = len - 1; i >= 0; i--) {
963             int digit = Character.digit(stPassed.charAt(i), 10);
964             digit *= (mul == 1) ? mul++ : mul--;
965             sum += (digit >= 10) ? (digit % 10) + 1 : digit;
966         }
967
968         return sum;
969     }
970
971     public static int getLuhnCheckDigit(String JavaDoc stPassed) {
972         int sum = getLuhnSum(stPassed);
973         int mod = ((sum / 10 + 1) * 10 - sum) % 10;
974         return (10 - mod);
975     }
976
977     public static boolean sumIsMod10(int sum) {
978         return ((sum % 10) == 0);
979     }
980
981     public static String JavaDoc appendCheckDigit(String JavaDoc stPassed) {
982         String JavaDoc checkDigit = new Integer JavaDoc(getLuhnCheckDigit(stPassed)).toString();
983         return stPassed + checkDigit;
984     }
985
986     /** Checks credit card number with Luhn Mod-10 test
987      *
988      * @param stPassed a string representing a credit card number
989      * @return true, if the credit card number passes the Luhn Mod-10 test, false otherwise
990      */

991     public static boolean isCreditCard(String JavaDoc stPassed) {
992         if (isEmpty(stPassed)) return defaultEmptyOK;
993         String JavaDoc st = stripCharsInBag(stPassed, creditCardDelimiters);
994
995         // encoding only works on cars with less the 19 digits
996
if (st.length() > 19) return false;
997         return sumIsMod10(getLuhnSum(st));
998     }
999
1000    /** Checks to see if the cc number is a valid Visa number
1001     *
1002     * @param cc a string representing a credit card number; Sample number: 4111 1111 1111 1111(16 digits)
1003     * @return true, if the credit card number is a valid VISA number, false otherwise
1004     */

1005    public static boolean isVisa(String JavaDoc cc) {
1006        if (((cc.length() == 16) || (cc.length() == 13)) && (cc.substring(0, 1).equals("4")))
1007            return isCreditCard(cc);
1008        return false;
1009    }
1010
1011    /** Checks to see if the cc number is a valid Master Card number
1012     *
1013     * @param cc a string representing a credit card number; Sample number: 5500 0000 0000 0004(16 digits)
1014     * @return true, if the credit card number is a valid MasterCard number, false otherwise
1015     */

1016    public static boolean isMasterCard(String JavaDoc cc) {
1017        int firstdig = Integer.parseInt(cc.substring(0, 1));
1018        int seconddig = Integer.parseInt(cc.substring(1, 2));
1019
1020        if ((cc.length() == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
1021            return isCreditCard(cc);
1022        return false;
1023
1024    }
1025
1026    /** Checks to see if the cc number is a valid American Express number
1027     * @param cc - a string representing a credit card number; Sample number: 340000000000009(15 digits)
1028     * @return true, if the credit card number is a valid American Express number, false otherwise
1029     */

1030    public static boolean isAmericanExpress(String JavaDoc cc) {
1031        int firstdig = Integer.parseInt(cc.substring(0, 1));
1032        int seconddig = Integer.parseInt(cc.substring(1, 2));
1033
1034        if ((cc.length() == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
1035            return isCreditCard(cc);
1036        return false;
1037
1038    }
1039
1040    /** Checks to see if the cc number is a valid Diners Club number
1041     * @param cc - a string representing a credit card number; Sample number: 30000000000004(14 digits)
1042     * @return true, if the credit card number is a valid Diner's Club number, false otherwise
1043     */

1044    public static boolean isDinersClub(String JavaDoc cc) {
1045        int firstdig = Integer.parseInt(cc.substring(0, 1));
1046        int seconddig = Integer.parseInt(cc.substring(1, 2));
1047
1048        if ((cc.length() == 14) && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
1049            return isCreditCard(cc);
1050        return false;
1051    }
1052
1053    /** Checks to see if the cc number is a valid Carte Blanche number
1054     * @param cc - a string representing a credit card number; Sample number: 30000000000004(14 digits)
1055     * @return true, if the credit card number is a valid Carte Blanche number, false otherwise
1056     */

1057    public static boolean isCarteBlanche(String JavaDoc cc) {
1058        return isDinersClub(cc);
1059    }
1060
1061    /** Checks to see if the cc number is a valid Discover number
1062     * @param cc - a string representing a credit card number; Sample number: 6011000000000004(16 digits)
1063     * @return true, if the credit card number is a valid Discover card number, false otherwise
1064     */

1065    public static boolean isDiscover(String JavaDoc cc) {
1066        String JavaDoc first4digs = cc.substring(0, 4);
1067
1068        if ((cc.length() == 16) && (first4digs.equals("6011")))
1069            return isCreditCard(cc);
1070        return false;
1071    }
1072
1073    /** Checks to see if the cc number is a valid EnRoute number
1074     * @param cc - a string representing a credit card number; Sample number: 201400000000009(15 digits)
1075     * @return true, if the credit card number is a valid enRoute card number, false, otherwise
1076     */

1077    public static boolean isEnRoute(String JavaDoc cc) {
1078        String JavaDoc first4digs = cc.substring(0, 4);
1079
1080        if ((cc.length() == 15) && (first4digs.equals("2014") || first4digs.equals("2149")))
1081            return isCreditCard(cc);
1082        return false;
1083    }
1084
1085    /** Checks to see if the cc number is a valid JCB number
1086     * @param cc - a string representing a credit card number; Sample number: 3088000000000009(16 digits)
1087     * @return true, if the credit card number is a valid JCB card number, false otherwise
1088     */

1089    public static boolean isJCB(String JavaDoc cc) {
1090        String JavaDoc first4digs = cc.substring(0, 4);
1091
1092        if ((cc.length() == 16) &&
1093            (first4digs.equals("3088") ||
1094                first4digs.equals("3096") ||
1095                first4digs.equals("3112") ||
1096                first4digs.equals("3158") ||
1097                first4digs.equals("3337") ||
1098                first4digs.equals("3528")))
1099            return isCreditCard(cc);
1100        return false;
1101    }
1102
1103    /** Checks to see if the cc number is a valid number for any accepted credit card
1104     * @param ccPassed - a string representing a credit card number
1105     * @return true, if the credit card number is any valid credit card number for any of the accepted card types, false otherwise
1106     */

1107    public static boolean isAnyCard(String JavaDoc ccPassed) {
1108        if (isEmpty(ccPassed)) return defaultEmptyOK;
1109
1110        String JavaDoc cc = stripCharsInBag(ccPassed, creditCardDelimiters);
1111
1112        if (!isCreditCard(cc)) return false;
1113        if (isMasterCard(cc) || isVisa(cc) || isAmericanExpress(cc) || isDinersClub(cc) ||
1114                isDiscover(cc) || isEnRoute(cc) || isJCB(cc))
1115            return true;
1116        return false;
1117    }
1118
1119    /** Checks to see if the cc number is a valid number for any accepted credit card, and return the name of that type
1120     * @param ccPassed - a string representing a credit card number
1121     * @return true, if the credit card number is any valid credit card number for any of the accepted card types, false otherwise
1122     */

1123    public static String JavaDoc getCardType(String JavaDoc ccPassed) {
1124        if (isEmpty(ccPassed)) return "Unknown";
1125        String JavaDoc cc = stripCharsInBag(ccPassed, creditCardDelimiters);
1126
1127        if (!isCreditCard(cc)) return "Unknown";
1128
1129        if (isMasterCard(cc)) return "MasterCard";
1130        if (isVisa(cc)) return "Visa";
1131        if (isAmericanExpress(cc)) return "AmericanExpress";
1132        if (isDinersClub(cc)) return "DinersClub";
1133        if (isDiscover(cc)) return "Discover";
1134        if (isEnRoute(cc)) return "EnRoute";
1135        if (isJCB(cc)) return "JCB";
1136        return "Unknown";
1137    }
1138
1139    /** Checks to see if the cc number is a valid number for the specified type
1140     * @param cardType - a string representing the credit card type
1141     * @param cardNumberPassed - a string representing a credit card number
1142     * @return true, if the credit card number is valid for the particular credit card type given in "cardType", false otherwise
1143     */

1144    public static boolean isCardMatch(String JavaDoc cardType, String JavaDoc cardNumberPassed) {
1145        if (isEmpty(cardType)) return defaultEmptyOK;
1146        if (isEmpty(cardNumberPassed)) return defaultEmptyOK;
1147        String JavaDoc cardNumber = stripCharsInBag(cardNumberPassed, creditCardDelimiters);
1148
1149        if ((cardType.equalsIgnoreCase("VISA")) && (isVisa(cardNumber))) return true;
1150        if ((cardType.equalsIgnoreCase("MASTERCARD")) && (isMasterCard(cardNumber))) return true;
1151        if (((cardType.equalsIgnoreCase("AMERICANEXPRESS")) || (cardType.equalsIgnoreCase("AMEX"))) && (isAmericanExpress(cardNumber))) return true;
1152        if ((cardType.equalsIgnoreCase("DISCOVER")) && (isDiscover(cardNumber))) return true;
1153        if ((cardType.equalsIgnoreCase("JCB")) && (isJCB(cardNumber))) return true;
1154        if (((cardType.equalsIgnoreCase("DINERSCLUB")) || (cardType.equalsIgnoreCase("DINERS"))) && (isDinersClub(cardNumber))) return true;
1155        if ((cardType.equalsIgnoreCase("CARTEBLANCHE")) && (isCarteBlanche(cardNumber))) return true;
1156        if ((cardType.equalsIgnoreCase("ENROUTE")) && (isEnRoute(cardNumber))) return true;
1157        return false;
1158    }
1159
1160
1161    /** isNotPoBox returns true if address argument does not contain anything that looks like a a PO Box. */
1162    public static boolean isNotPoBox(String JavaDoc s) {
1163        if (isEmpty(s)) return defaultEmptyOK;
1164
1165        // strings to check from Greg's program
1166
// "P.O. B"
1167
// "P.o.B"
1168
// "P.O B"
1169
// "PO. B"
1170
// "P O B"
1171
// "PO B"
1172
// "P.0. B"
1173
// "P0 B"
1174

1175        String JavaDoc sl = s.toLowerCase();
1176        if (sl.indexOf("p.o. b") != -1) return false;
1177        if (sl.indexOf("p.o.b") != -1) return false;
1178        if (sl.indexOf("p.o b") != -1) return false;
1179        if (sl.indexOf("p o b") != -1) return false;
1180        if (sl.indexOf("po b") != -1) return false;
1181        if (sl.indexOf("pobox") != -1) return false;
1182        if (sl.indexOf("po#") != -1) return false;
1183        if (sl.indexOf("po #") != -1) return false;
1184
1185        // now with 0's for them sneaky folks
1186
if (sl.indexOf("p.0. b") != -1) return false;
1187        if (sl.indexOf("p.0.b") != -1) return false;
1188        if (sl.indexOf("p.0 b") != -1) return false;
1189        if (sl.indexOf("p 0 b") != -1) return false;
1190        if (sl.indexOf("p0 b") != -1) return false;
1191        if (sl.indexOf("p0box") != -1) return false;
1192        if (sl.indexOf("p0#") != -1) return false;
1193        if (sl.indexOf("p0 #") != -1) return false;
1194        return true;
1195    }
1196}
1197
Popular Tags