String[] groups = ipv4Validator.match(inet4Address);
@Override // must return full string public String validate(String value) { if (super.match(value) != null) { int length = value.length(); for(CreditCardRange range : ccr) { if (validLength(length, range)) { if (range.high == null) { // single prefix only if (value.startsWith(range.low)) { return value; } } else if (range.low.compareTo(value) <= 0 // no need to trim value here && // here we have to ignore digits beyond the prefix range.high.compareTo(value.substring(0, range.high.length())) >= 0) { return value; } } } } return null; } @Override
final boolean isValidDomainSyntax(String domain) { if (domain == null) { return false; } domain = unicodeToASCII(domain); // hosts must be equally reachable via punycode and Unicode; // Unicode is never shorter than punycode, so check punycode // if domain did not convert, then it will be caught by ASCII // checks in the regexes below if (domain.length() > MAX_DOMAIN_LENGTH) { return false; } String[] groups = domainRegex.match(domain); return (groups != null && groups.length > 0) || hostnameRegex.isValid(domain); }
/** * Returns true if the specified <code>String</code> parses * as a valid domain name with a recognized top-level domain. * The parsing is case-insensitive. * @param domain the parameter to check for domain name syntax * @return true if the parameter is a valid domain name */ public boolean isValid(String domain) { if (domain == null) { return false; } domain = unicodeToASCII(domain); // hosts must be equally reachable via punycode and Unicode; // Unicode is never shorter than punycode, so check punycode // if domain did not convert, then it will be caught by ASCII // checks in the regexes below if (domain.length() > MAX_DOMAIN_LENGTH) { return false; } String[] groups = domainRegex.match(domain); if (groups != null && groups.length > 0) { return isValidTld(groups[0]); } return allowLocal && hostnameRegex.isValid(domain); }
/** * Test Null value */ public void testNullValue() { RegexValidator validator = new RegexValidator(REGEX); assertEquals("Instance isValid()", false, validator.isValid(null)); assertEquals("Instance validate()", null, validator.validate(null)); assertEquals("Instance match()", null, validator.match(null)); }
/** * Test instance methods with single regular expression. */ public void testSingle() { RegexValidator sensitive = new RegexValidator(REGEX); RegexValidator insensitive = new RegexValidator(REGEX, false); // isValid() assertEquals("Sensitive isValid() valid", true, sensitive.isValid("ac-DE-1")); assertEquals("Sensitive isValid() invalid", false, sensitive.isValid("AB-de-1")); assertEquals("Insensitive isValid() valid", true, insensitive.isValid("AB-de-1")); assertEquals("Insensitive isValid() invalid", false, insensitive.isValid("ABd-de-1")); // validate() assertEquals("Sensitive validate() valid", "acDE1", sensitive.validate("ac-DE-1")); assertEquals("Sensitive validate() invalid", null, sensitive.validate("AB-de-1")); assertEquals("Insensitive validate() valid", "ABde1", insensitive.validate("AB-de-1")); assertEquals("Insensitive validate() invalid", null, insensitive.validate("ABd-de-1")); // match() checkArray("Sensitive match() valid", new String[] {"ac", "DE", "1"}, sensitive.match("ac-DE-1")); checkArray("Sensitive match() invalid", null, sensitive.match("AB-de-1")); checkArray("Insensitive match() valid", new String[] {"AB", "de", "1"}, insensitive.match("AB-de-1")); checkArray("Insensitive match() invalid", null, insensitive.match("ABd-de-1")); assertEquals("validate one", "ABC", (new RegexValidator("^([A-Z]*)$")).validate("ABC")); checkArray("match one", new String[] {"ABC"}, (new RegexValidator("^([A-Z]*)$")).match("ABC")); }
checkArray("match() Multiple", array, multiple.match(value)); checkArray("match() 1st", null, single1.match(value)); checkArray("match() 2nd", array, single2.match(value)); checkArray("match() 3rd", null, single3.match(value)); assertEquals("isValid() Invalid", false, multiple.isValid(value)); assertEquals("validate() Invalid", null, multiple.validate(value)); assertEquals("match() Multiple", null, multiple.match(value));
checkArray("Sensitive match() Multiple", array, multiple.match(value)); checkArray("Sensitive match() 1st", null, single1.match(value)); checkArray("Sensitive match() 2nd", array, single2.match(value)); checkArray("Sensitive match() 3rd", null, single3.match(value)); assertEquals("isValid() Invalid", false, multiple.isValid(value)); assertEquals("validate() Invalid", null, multiple.validate(value)); assertEquals("match() Multiple", null, multiple.match(value));
private boolean isValidDomain(String domain) { String[] groups = domainRegex.match(domain); if (groups != null && groups.length > 0) { return true; } else { if (hostnameRegex.isValid(domain)) { return true; } } return false; }
private boolean isValidDomain(String domain) { String[] groups = domainRegex.match(domain); if (groups != null && groups.length > 0) { return true; } else { if (hostnameRegex.isValid(domain)) { return true; } } return false; }
final boolean isValidDomainSyntax(String domain) { if (domain == null) { return false; } domain = unicodeToASCII(domain); // hosts must be equally reachable via punycode and Unicode; // Unicode is never shorter than punycode, so check punycode // if domain did not convert, then it will be caught by ASCII // checks in the regexes below if (domain.length() > MAX_DOMAIN_LENGTH) { return false; } String[] groups = domainRegex.match(domain); return (groups != null && groups.length > 0 && completeDomainRegex.match(domain) != null) || hostnameRegex.isValid(domain); }
String[] groups = ipv4Validator.match(inet4Address);
@Override // must return full string public String validate(String value) { if (super.match(value) != null) { int length = value.length(); for(CreditCardRange range : ccr) { if (validLength(length, range)) { if (range.high == null) { // single prefix only if (value.startsWith(range.low)) { return value; } } else if (range.low.compareTo(value) <= 0 // no need to trim value here && // here we have to ignore digits beyond the prefix range.high.compareTo(value.substring(0, range.high.length())) >= 0) { return value; } } } } return null; } @Override
@Override // must return full string public String validate(String value) { if (super.match(value) != null) { int length = value.length(); for(CreditCardRange range : ccr) { if (validLength(length, range)) { if (range.high == null) { // single prefix only if (value.startsWith(range.low)) { return value; } } else if (range.low.compareTo(value) <= 0 // no need to trim value here && // here we have to ignore digits beyond the prefix range.high.compareTo(value.substring(0, range.high.length())) >= 0) { return value; } } } } return null; } @Override
// Copied from org.apache.commons.validator.routines.DomainValidator private static final String DOMAIN_LABEL_REGEX = "\\p{Alnum}(?>[\\p{Alnum}-]*\\p{Alnum})*"; // Changed to include new gTLD - http://data.iana.org/TLD/tlds-alpha-by-domain.txt private static final String TOP_LABEL_REGEX = "\\p{Alpha}[\\p{Alnum}-]*\\p{Alpha}"; // Copied from org.apache.commons.validator.routines.DomainValidator private static final String DOMAIN_NAME_REGEX = "^(?:" + DOMAIN_LABEL_REGEX + "\\.)+" + "(" + TOP_LABEL_REGEX + ")$"; private static final RegexValidator domainRegex = new RegexValidator(DOMAIN_NAME_REGEX); private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(); public static boolean isValidDomain(String domain) { String[] groups = domainRegex.match(domain); return groups != null && groups.length > 0; }
/** * Returns true if the specified <code>String</code> parses * as a valid domain name with a recognized top-level domain. * The parsing is case-insensitive. * @param domain the parameter to check for domain name syntax * @return true if the parameter is a valid domain name */ public boolean isValid(String domain) { if (domain == null) { return false; } domain = unicodeToASCII(domain); // hosts must be equally reachable via punycode and Unicode; // Unicode is never shorter than punycode, so check punycode // if domain did not convert, then it will be caught by ASCII // checks in the regexes below if (domain.length() > MAX_DOMAIN_LENGTH) { return false; } String[] groups = domainRegex.match(domain); if (groups != null && groups.length > 0 && completeDomainRegex.match(domain) != null) { return isValidTld(groups[0]); } return allowLocal && hostnameRegex.isValid(domain); }