List of usage examples for org.apache.commons.validator.routines CreditCardValidator CreditCardValidator
public CreditCardValidator(CodeValidator[] creditCardValidators)
From source file:com.threew.validacion.tarjetas.credito.utilidad.Validador.java
/*** * Valida a partir de la propiedad nmero de tarjeta * * @return boolean/* www .j a v a 2s. com*/ **/ public boolean validar() { if (prevalidarTarjeta(this.numeroTarjetaString) == Boolean.FALSE) { return Boolean.FALSE; } /// Crear instancia de validador de Apache CreditCardValidator validador = new CreditCardValidator( CreditCardValidator.AMEX + CreditCardValidator.VISA + CreditCardValidator.MASTERCARD); /// Procesar y retornar return validador.isValid(this.numeroTarjetaString); }
From source file:com.github.pjungermann.config.specification.constraint.creditCard.CreditCardConstraint.java
@SuppressWarnings("unchecked") protected CreditCardValidator getValidator() { assert expectation != null; if (expectation instanceof CharSequence) { return new CreditCardValidator(TYPES.get(expectation.toString().toLowerCase(ENGLISH))); }/*from ww w.j a v a 2 s. co m*/ final Collection<String> selection; if (expectation instanceof Boolean) { selection = (Boolean) expectation ? TYPES.keySet() : Collections.<String>emptyList(); } else { selection = (Collection<String>) expectation; } long options = 0L; for (final String type : selection) { options += TYPES.get(type.toLowerCase(ENGLISH)); } return new CreditCardValidator(options); }
From source file:com.threew.validacion.tarjetas.credito.utilidad.Validador.java
/*** * Valida a partir de la propiedad nmero de tarjeta * @param numero Es el numero de tarjeta en formato string. * @return boolean/*w w w . jav a2 s.c om*/ **/ public boolean validar(String numero) { if (prevalidarTarjeta(numero) == Boolean.FALSE) { return Boolean.FALSE; } /// Crear instancia de validador de Apache CreditCardValidator validador = new CreditCardValidator( CreditCardValidator.AMEX + CreditCardValidator.VISA + CreditCardValidator.MASTERCARD); /// Procesar y retornar return validador.isValid(numero); }
From source file:org.lirazs.gbackbone.validation.client.rule.CreditCardRule.java
@Override public boolean isValid(final String creditCardNumber, String attribute) { CreditCard.Type[] types = ruleAnnotation.cardTypes(); HashSet<CreditCard.Type> typesSet = new HashSet<CreditCard.Type>(Arrays.asList(types)); long options = 0; if (!typesSet.contains(CreditCard.Type.NONE)) { for (CreditCard.Type type : typesSet) { options += CARD_TYPE_REGISTRY.get(type); }/* w ww . j a v a 2 s. co m*/ } else { options = CreditCardValidator.NONE; } return new CreditCardValidator(options).isValid(creditCardNumber.replaceAll("\\s", "")); }
From source file:org.moqui.impl.service.ServiceDefinition.java
private boolean validateParameterSingle(MNode valNode, String parameterName, Object pv, ExecutionContextImpl eci) {// ww w .java 2s .c o m // should never be null (caller checks) but check just in case if (pv == null) return true; String validateName = valNode.getName(); if ("val-or".equals(validateName)) { boolean anyPass = false; for (MNode child : valNode.getChildren()) if (validateParameterSingle(child, parameterName, pv, eci)) anyPass = true; return anyPass; } else if ("val-and".equals(validateName)) { boolean allPass = true; for (MNode child : valNode.getChildren()) if (!validateParameterSingle(child, parameterName, pv, eci)) allPass = false; return allPass; } else if ("val-not".equals(validateName)) { boolean allPass = true; for (MNode child : valNode.getChildren()) if (!validateParameterSingle(child, parameterName, pv, eci)) allPass = false; return !allPass; } else if ("matches".equals(validateName)) { if (!(pv instanceof CharSequence)) { Map<String, Object> map = new HashMap<>(1); map.put("pv", pv); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}) is not a string, cannot do matches validation.", "", map), null); return false; } String pvString = pv.toString(); String regexp = valNode.attribute("regexp"); if (regexp != null && !regexp.isEmpty() && !pvString.matches(regexp)) { // a message attribute should always be there, but just in case we'll have a default final String message = valNode.attribute("message"); Map<String, Object> map = new HashMap<>(2); map.put("pv", pv); map.put("regexp", regexp); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource() .expand(message != null && !message.isEmpty() ? message : "Value entered (${pv}) did not match expression: ${regexp}", "", map), null); return false; } return true; } else if ("number-range".equals(validateName)) { BigDecimal bdVal = new BigDecimal(pv.toString()); String minStr = valNode.attribute("min"); if (minStr != null && !minStr.isEmpty()) { BigDecimal min = new BigDecimal(minStr); if ("false".equals(valNode.attribute("min-include-equals"))) { if (bdVal.compareTo(min) <= 0) { Map<String, Object> map = new HashMap<>(2); map.put("pv", pv); map.put("min", min); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}) is less than or equal to ${min} must be greater than.", "", map), null); return false; } } else { if (bdVal.compareTo(min) < 0) { Map<String, Object> map = new HashMap<>(2); map.put("pv", pv); map.put("min", min); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}) is less than ${min} and must be greater than or equal to.", "", map), null); return false; } } } String maxStr = valNode.attribute("max"); if (maxStr != null && !maxStr.isEmpty()) { BigDecimal max = new BigDecimal(maxStr); if ("true".equals(valNode.attribute("max-include-equals"))) { if (bdVal.compareTo(max) > 0) { Map<String, Object> map = new HashMap<>(2); map.put("pv", pv); map.put("max", max); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}) is greater than ${max} and must be less than or equal to.", "", map), null); return false; } } else { if (bdVal.compareTo(max) >= 0) { Map<String, Object> map = new HashMap<>(2); map.put("pv", pv); map.put("max", max); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}) is greater than or equal to ${max} and must be less than.", "", map), null); return false; } } } return true; } else if ("number-integer".equals(validateName)) { try { new BigInteger(pv.toString()); } catch (NumberFormatException e) { if (logger.isTraceEnabled()) logger.trace( "Adding error message for NumberFormatException for BigInteger parse: " + e.toString()); Map<String, Object> map = new HashMap<>(1); map.put("pv", pv); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value [${pv}] is not a whole (integer) number.", "", map), null); return false; } return true; } else if ("number-decimal".equals(validateName)) { try { new BigDecimal(pv.toString()); } catch (NumberFormatException e) { if (logger.isTraceEnabled()) logger.trace( "Adding error message for NumberFormatException for BigDecimal parse: " + e.toString()); Map<String, Object> map = new HashMap<>(1); map.put("pv", pv); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value [${pv}] is not a decimal number.", "", map), null); return false; } return true; } else if ("text-length".equals(validateName)) { String str = pv.toString(); String minStr = valNode.attribute("min"); if (minStr != null && !minStr.isEmpty()) { int min = Integer.parseInt(minStr); if (str.length() < min) { Map<String, Object> map = new HashMap<>(3); map.put("pv", pv); map.put("str", str); map.put("minStr", minStr); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}), length ${str.length()}, is shorter than ${minStr} characters.", "", map), null); return false; } } String maxStr = valNode.attribute("max"); if (maxStr != null && !maxStr.isEmpty()) { int max = Integer.parseInt(maxStr); if (str.length() > max) { Map<String, Object> map = new HashMap<>(3); map.put("pv", pv); map.put("str", str); map.put("maxStr", maxStr); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand( "Value entered (${pv}), length ${str.length()}, is longer than ${maxStr} characters.", "", map), null); return false; } } return true; } else if ("text-email".equals(validateName)) { String str = pv.toString(); if (!emailValidator.isValid(str)) { Map<String, String> map = new HashMap<>(1); map.put("str", str); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value entered (${str}) is not a valid email address.", "", map), null); return false; } return true; } else if ("text-url".equals(validateName)) { String str = pv.toString(); if (!urlValidator.isValid(str)) { Map<String, String> map = new HashMap<>(1); map.put("str", str); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value entered (${str}) is not a valid URL.", "", map), null); return false; } return true; } else if ("text-letters".equals(validateName)) { String str = pv.toString(); for (char c : str.toCharArray()) { if (!Character.isLetter(c)) { Map<String, String> map = new HashMap<>(1); map.put("str", str); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value entered (${str}) must have only letters.", "", map), null); return false; } } return true; } else if ("text-digits".equals(validateName)) { String str = pv.toString(); for (char c : str.toCharArray()) { if (!Character.isDigit(c)) { Map<String, String> map = new HashMap<>(1); map.put("str", str); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value [${str}] must have only digits.", "", map), null); return false; } } return true; } else if ("time-range".equals(validateName)) { Calendar cal; String format = valNode.attribute("format"); if (pv instanceof CharSequence) { cal = eci.getL10n().parseDateTime(pv.toString(), format); } else { // try letting groovy convert it cal = Calendar.getInstance(); // TODO: not sure if this will work: ((pv as java.util.Date).getTime()) cal.setTimeInMillis((DefaultGroovyMethods.asType(pv, Date.class)).getTime()); } String after = valNode.attribute("after"); if (after != null && !after.isEmpty()) { // handle after date/time/date-time depending on type of parameter, support "now" too Calendar compareCal; if ("now".equals(after)) { compareCal = Calendar.getInstance(); compareCal.setTimeInMillis(eci.getUser().getNowTimestamp().getTime()); } else { compareCal = eci.getL10n().parseDateTime(after, format); } if (cal != null && !cal.after(compareCal)) { Map<String, Object> map = new HashMap<>(2); map.put("pv", pv); map.put("after", after); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value entered (${pv}) is before ${after}.", "", map), null); return false; } } String before = valNode.attribute("before"); if (before != null && !before.isEmpty()) { // handle after date/time/date-time depending on type of parameter, support "now" too Calendar compareCal; if ("now".equals(before)) { compareCal = Calendar.getInstance(); compareCal.setTimeInMillis(eci.getUser().getNowTimestamp().getTime()); } else { compareCal = eci.getL10n().parseDateTime(before, format); } if (cal != null && !cal.before(compareCal)) { Map<String, Object> map = new HashMap<>(1); map.put("pv", pv); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource().expand("Value entered (${pv}) is after ${before}.", "", map), null); return false; } } return true; } else if ("credit-card".equals(validateName)) { long creditCardTypes = 0; String types = valNode.attribute("types"); if (types != null && !types.isEmpty()) { for (String cts : types.split(",")) creditCardTypes += creditCardTypeMap.get(cts.trim()); } else { creditCardTypes = allCreditCards; } CreditCardValidator ccv = new CreditCardValidator(creditCardTypes); String str = pv.toString(); if (!ccv.isValid(str)) { Map<String, String> map = new HashMap<>(1); map.put("str", str); eci.getMessage().addValidationError(null, parameterName, serviceName, eci.getResource() .expand("Value entered (${str}) is not a valid credit card number.", "", map), null); return false; } return true; } // shouldn't get here, but just in case return true; }
From source file:org.mule.modules.validation.ValidationModule.java
/** * If if the specified <code>creditCardNumber</code> is not a valid credit card number throw an exception. * <p/>// w w w.j a v a2 s . c om * {@sample.xml ../../../doc/mule-module-validation.xml.sample validation:validate-credit-card-number} * * @param creditCardNumber Credit card number to validate * @param creditCardTypes Credit card types to validate * @param customExceptionClassName Class name of the exception to throw * @throws Exception if not valid */ @Processor public void validateCreditCardNumber(String creditCardNumber, List<CreditCardType> creditCardTypes, @Optional @Default("org.mule.modules.validation.InvalidException") String customExceptionClassName) throws Exception { CodeValidator[] validators = new CodeValidator[creditCardTypes.size()]; int i = 0; for (CreditCardType type : creditCardTypes) { validators[i] = type.getCodeValidator(); i++; } CreditCardValidator validator = new CreditCardValidator(validators); if (validator.validate(creditCardNumber) == null) { throw buildException(customExceptionClassName); } }