一、前言
基于apache的commons-validator源码包中的org.apache.commons.validator.GenericValidator类,对常见验证如是否为空isBlankOrNull、是否对应正则匹配的规则matchRegexp、是否基本的数据类型isByte(是否字节类型)/isShort/isInt(是否整型)/isLong(是否长整型)/isDouble、是否日期类型isDate、是否在指定范围内isInRange、是否为邮件地址格式isEmail、是否URL格式isUrl、是否为最大长度maxLength/最小长度minLength、是否最大值maxValue/最小值minValue等
二、源码说明
1.GenericValidator类,UrlValidator参考其他文章页
package org.apache.commons.validator;@b@@b@import java.io.Serializable;@b@import java.util.Locale;@b@import org.apache.oro.text.perl.Perl5Util;@b@@b@public class GenericValidator@b@ implements Serializable@b@{@b@ private static final UrlValidator URL_VALIDATOR = new UrlValidator();@b@ private static final CreditCardValidator CREDIT_CARD_VALIDATOR = new CreditCardValidator();@b@@b@ public static boolean isBlankOrNull(String value)@b@ {@b@ return ((value == null) || (value.trim().length() == 0));@b@ }@b@@b@ public static boolean matchRegexp(String value, String regexp)@b@ {@b@ if ((regexp == null) || (regexp.length() <= 0)) {@b@ return false;@b@ }@b@@b@ Perl5Util matcher = new Perl5Util();@b@ return matcher.match("/" + regexp + "/", value);@b@ }@b@@b@ public static boolean isByte(String value)@b@ {@b@ return (GenericTypeValidator.formatByte(value) != null);@b@ }@b@@b@ public static boolean isShort(String value)@b@ {@b@ return (GenericTypeValidator.formatShort(value) != null);@b@ }@b@@b@ public static boolean isInt(String value)@b@ {@b@ return (GenericTypeValidator.formatInt(value) != null);@b@ }@b@@b@ public static boolean isLong(String value)@b@ {@b@ return (GenericTypeValidator.formatLong(value) != null);@b@ }@b@@b@ public static boolean isFloat(String value)@b@ {@b@ return (GenericTypeValidator.formatFloat(value) != null);@b@ }@b@@b@ public static boolean isDouble(String value)@b@ {@b@ return (GenericTypeValidator.formatDouble(value) != null);@b@ }@b@@b@ public static boolean isDate(String value, Locale locale)@b@ {@b@ return DateValidator.getInstance().isValid(value, locale);@b@ }@b@@b@ public static boolean isDate(String value, String datePattern, boolean strict)@b@ {@b@ return DateValidator.getInstance().isValid(value, datePattern, strict);@b@ }@b@@b@ public static boolean isInRange(byte value, byte min, byte max)@b@ {@b@ return ((value >= min) && (value <= max));@b@ }@b@@b@ public static boolean isInRange(int value, int min, int max)@b@ {@b@ return ((value >= min) && (value <= max));@b@ }@b@@b@ public static boolean isInRange(float value, float min, float max)@b@ {@b@ return ((value >= min) && (value <= max));@b@ }@b@@b@ public static boolean isInRange(short value, short min, short max)@b@ {@b@ return ((value >= min) && (value <= max));@b@ }@b@@b@ public static boolean isInRange(long value, long min, long max)@b@ {@b@ return ((value >= min) && (value <= max));@b@ }@b@@b@ public static boolean isInRange(double value, double min, double max)@b@ {@b@ return ((value >= min) && (value <= max));@b@ }@b@@b@ public static boolean isCreditCard(String value)@b@ {@b@ return CREDIT_CARD_VALIDATOR.isValid(value);@b@ }@b@@b@ public static boolean isEmail(String value)@b@ {@b@ return EmailValidator.getInstance().isValid(value);@b@ }@b@@b@ public static boolean isUrl(String value)@b@ {@b@ return URL_VALIDATOR.isValid(value);@b@ }@b@@b@ public static boolean maxLength(String value, int max)@b@ {@b@ return (value.length() <= max);@b@ }@b@@b@ public static boolean maxLength(String value, int max, int lineEndLength)@b@ {@b@ int adjustAmount = adjustForLineEnding(value, lineEndLength);@b@ return (value.length() + adjustAmount <= max);@b@ }@b@@b@ public static boolean minLength(String value, int min)@b@ {@b@ return (value.length() >= min);@b@ }@b@@b@ public static boolean minLength(String value, int min, int lineEndLength)@b@ {@b@ int adjustAmount = adjustForLineEnding(value, lineEndLength);@b@ return (value.length() + adjustAmount >= min);@b@ }@b@@b@ private static int adjustForLineEnding(String value, int lineEndLength)@b@ {@b@ int nCount = 0;@b@ int rCount = 0;@b@ for (int i = 0; i < value.length(); ++i) {@b@ if (value.charAt(i) == '\n')@b@ ++nCount;@b@@b@ if (value.charAt(i) == '\r')@b@ ++rCount;@b@ }@b@@b@ return (nCount * lineEndLength - rCount + nCount);@b@ }@b@@b@ public static boolean minValue(int value, int min)@b@ {@b@ return (value >= min);@b@ }@b@@b@ public static boolean minValue(long value, long min)@b@ {@b@ return (value >= min);@b@ }@b@@b@ public static boolean minValue(double value, double min)@b@ {@b@ return (value >= min);@b@ }@b@@b@ public static boolean minValue(float value, float min)@b@ {@b@ return (value >= min);@b@ }@b@@b@ public static boolean maxValue(int value, int max)@b@ {@b@ return (value <= max);@b@ }@b@@b@ public static boolean maxValue(long value, long max)@b@ {@b@ return (value <= max);@b@ }@b@@b@ public static boolean maxValue(double value, double max)@b@ {@b@ return (value <= max);@b@ }@b@@b@ public static boolean maxValue(float value, float max)@b@ {@b@ return (value <= max);@b@ }@b@}
2.CreditCardValidator类
package org.apache.commons.validator;@b@@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Iterator;@b@import org.apache.commons.validator.util.Flags;@b@@b@public class CreditCardValidator@b@{@b@ public static final int NONE = 0;@b@ public static final int AMEX = 1;@b@ public static final int VISA = 2;@b@ public static final int MASTERCARD = 4;@b@ public static final int DISCOVER = 8;@b@ private Collection cardTypes;@b@@b@ public CreditCardValidator()@b@ {@b@ this(15);@b@ }@b@@b@ public CreditCardValidator(int options)@b@ {@b@ this.cardTypes = new ArrayList();@b@@b@ Flags f = new Flags(options);@b@ if (f.isOn(2L)) {@b@ this.cardTypes.add(new Visa(this, null));@b@ }@b@@b@ if (f.isOn(1L)) {@b@ this.cardTypes.add(new Amex(this, null));@b@ }@b@@b@ if (f.isOn(4L)) {@b@ this.cardTypes.add(new Mastercard(this, null));@b@ }@b@@b@ if (f.isOn(8L))@b@ this.cardTypes.add(new Discover(this, null));@b@ }@b@@b@ public boolean isValid(String card)@b@ {@b@ if ((card == null) || (card.length() < 13) || (card.length() > 19)) {@b@ return false;@b@ }@b@@b@ if (!(luhnCheck(card))) {@b@ return false;@b@ }@b@@b@ Iterator types = this.cardTypes.iterator();@b@ while (types.hasNext()) {@b@ CreditCardType type = (CreditCardType)types.next();@b@ if (type.matches(card))@b@ return true;@b@@b@ }@b@@b@ return false;@b@ }@b@@b@ public void addAllowedCardType(CreditCardType type)@b@ {@b@ this.cardTypes.add(type);@b@ }@b@@b@ protected boolean luhnCheck(String cardNumber)@b@ {@b@ int digits = cardNumber.length();@b@ int oddOrEven = digits & 0x1;@b@ long sum = 0L;@b@ for (int count = 0; count < digits; ++count) {@b@ int digit = 0;@b@ try {@b@ digit = Integer.parseInt(cardNumber.charAt(count) + "");@b@ } catch (NumberFormatException e) {@b@ return false;@b@ }@b@@b@ if ((count & 0x1 ^ oddOrEven) == 0) {@b@ digit *= 2;@b@ if (digit > 9)@b@ digit -= 9;@b@ }@b@@b@ sum += digit;@b@ }@b@@b@ return (sum != 0L);@b@ }@b@@b@ private class Mastercard@b@ implements CreditCardValidator.CreditCardType@b@ {@b@ private static final String PREFIX = "51,52,53,54,55,";@b@ private final CreditCardValidator this$0;@b@@b@ private Mastercard()@b@ {@b@ this.this$0 = this$0; }@b@@b@ public boolean matches() {@b@ String prefix2 = card.substring(0, 2) + ",";@b@ return (("51,52,53,54,55,".indexOf(prefix2) != -1) && (card.length() == 16));@b@ }@b@@b@ Mastercard(, CreditCardValidator.1 x1)@b@ {@b@ this(x0);@b@ }@b@ }@b@@b@ private class Discover@b@ implements CreditCardValidator.CreditCardType@b@ {@b@ private static final String PREFIX = "6011";@b@ private final CreditCardValidator this$0;@b@@b@ private Discover()@b@ {@b@ this.this$0 = this$0; }@b@@b@ public boolean matches() {@b@ return ((card.substring(0, 4).equals("6011")) && (card.length() == 16));@b@ }@b@@b@ Discover(, CreditCardValidator.1 x1)@b@ {@b@ this(x0);@b@ }@b@ }@b@@b@ private class Amex@b@ implements CreditCardValidator.CreditCardType@b@ {@b@ private static final String PREFIX = "34,37,";@b@ private final CreditCardValidator this$0;@b@@b@ private Amex()@b@ {@b@ this.this$0 = this$0; }@b@@b@ public boolean matches() {@b@ String prefix2 = card.substring(0, 2) + ",";@b@ return (("34,37,".indexOf(prefix2) != -1) && (card.length() == 15));@b@ }@b@@b@ Amex(, CreditCardValidator.1 x1)@b@ {@b@ this(x0);@b@ }@b@ }@b@@b@ private class Visa@b@ implements CreditCardValidator.CreditCardType@b@ {@b@ private static final String PREFIX = "4";@b@ private final CreditCardValidator this$0;@b@@b@ private Visa()@b@ {@b@ this.this$0 = this$0; }@b@@b@ public boolean matches() {@b@ return ((card.substring(0, 1).equals("4")) && (((card.length() == 13) || (card.length() == 16))));@b@ }@b@@b@ Visa(, CreditCardValidator.1 x1)@b@ {@b@ this(x0);@b@ }@b@ }@b@@b@ public static abstract interface CreditCardType@b@ {@b@ public abstract boolean matches(String paramString);@b@ }@b@}