Class UtilValidate

java.lang.Object
org.apache.ofbiz.base.util.UtilValidate

public final class UtilValidate extends Object
General input/data validation methods Utility methods for validating data, especially input. See detailed description below.
SUMMARY

This is a set of meethods for validating input. Functions are provided to validate:
- U.S. and international phone/fax numbers
- U.S. ZIP codes(5 or 9 digit postal codes)
- U.S. Postal Codes(2 letter abbreviations for names of states)
- U.S. Social Security Numbers(abbreviated as SSNs)
- email addresses
- dates(entry of year, month, and day and validity of combined date)
- credit card numbers

Supporting utility functions validate that:
- characters are Letter, Digit, or LetterOrDigit
- strings are a Signed, Positive, Negative, Nonpositive, or Nonnegative integer
- strings are a Float or a SignedFloat
- strings are Alphabetic, Alphanumeric, or Whitespace
- strings contain an integer within a specified range

Other utility functions are provided to:
- remove from a string characters which are/are not in a "bag" of selected characters
- strip whitespace/leading whitespace from a string

==============================================================================
NOTE: This code was adapted from the Netscape JavaScript form validation code,
usually found in "FormChek.js". Credit card verification functions Originally
included as Starter Application 1.0.0 in LivePayment.
==============================================================================
  • Field Details

    • DIGITS

      public static final String DIGITS
      digit characters
      See Also:
    • LOWER_CASE_LETTERS

      public static final String LOWER_CASE_LETTERS
      lower-case letter characters
      See Also:
    • UPPER_CASE_LETTERS

      public static final String UPPER_CASE_LETTERS
      upper-case letter characters
      See Also:
    • LETTERS

      public static final String LETTERS
      letter characters
      See Also:
    • DECIMAL_POINT_DELIMITER

      public static final String DECIMAL_POINT_DELIMITER
      decimal point character differs by language and culture
      See Also:
    • PHONE_NUMBER_DELIMITERS

      public static final String PHONE_NUMBER_DELIMITERS
      non-digit characters which are allowed in phone numbers
      See Also:
    • VALID_US_PHONE_CHARS

      public static final String VALID_US_PHONE_CHARS
      characters which are allowed in US phone numbers
      See Also:
    • VALID_WORLD_PHONE_CHARS

      public static final String VALID_WORLD_PHONE_CHARS
      characters which are allowed in international phone numbers(a leading + is OK)
      See Also:
    • SSN_DELIMITERS

      public static final String SSN_DELIMITERS
      non-digit characters which are allowed in Social Security Numbers
      See Also:
    • VALID_SSN_CHARS

      public static final String VALID_SSN_CHARS
      characters which are allowed in Social Security Numbers
      See Also:
    • DIGITS_IN_SSN

      public static final int DIGITS_IN_SSN
      U.S. Social Security Numbers have 9 DIGITS. They are formatted as 123-45-6789.
      See Also:
    • DIGITS_IN_US_PHONE

      public static final int DIGITS_IN_US_PHONE
      U.S. phone numbers have 10 DIGITS. They are formatted as 123 456 7890 or(123) 456-7890.
      See Also:
    • DIGITS_IN_US_PHONE_AREA

      public static final int DIGITS_IN_US_PHONE_AREA
      See Also:
    • DIGITS_IN_US_PHONE_MAIN

      public static final int DIGITS_IN_US_PHONE_MAIN
      See Also:
    • ZIP_CODE_DELIMITERS

      public static final String ZIP_CODE_DELIMITERS
      non-digit characters which are allowed in ZIP Codes
      See Also:
    • ZIP_CODE_DELIMITER

      public static final String ZIP_CODE_DELIMITER
      our preferred delimiter for reformatting ZIP Codes
      See Also:
    • VALID_ZIP_CODE_CHARS

      public static final String VALID_ZIP_CODE_CHARS
      characters which are allowed in Social Security Numbers
      See Also:
    • DIGITS_IN_ZIP_CODE_1

      public static final int DIGITS_IN_ZIP_CODE_1
      U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789.
      See Also:
    • DIGITS_IN_ZIP_CODE_2

      public static final int DIGITS_IN_ZIP_CODE_2
      U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789.
      See Also:
    • CREDIT_CARD_DELIMITERS

      public static final String CREDIT_CARD_DELIMITERS
      non-digit characters which are allowed in credit card numbers
      See Also:
    • US_STATE_CODE_DELIMITER

      public static final String US_STATE_CODE_DELIMITER
      Delimiter for USStateCodes String
      See Also:
    • US_STATE_CODES

      public static final String US_STATE_CODES
      Valid U.S. Postal Codes for states, territories, armed forces, etc. See http://www.usps.gov/ncsc/lookups/abbr_state.txt.
      See Also:
    • CONTIGUOUS_US_STATE_CODES

      public static final String CONTIGUOUS_US_STATE_CODES
      Valid contiguous U.S. postal codes
      See Also:
  • Method Details

    • isEmpty

      public static boolean isEmpty(Object o)
      Check whether an object is empty, will see if it is a String, Map, Collection, etc.
    • isNotEmpty

      public static boolean isNotEmpty(Object o)
      Check whether an object is NOT empty, will see if it is a String, Map, Collection, etc.
    • isEmpty

      public static boolean isEmpty(IsEmpty o)
      Check whether IsEmpty o is empty.
    • isNotEmpty

      public static boolean isNotEmpty(IsEmpty o)
      Check whether IsEmpty o is NOT empty.
    • isEmpty

      public static <E> boolean isEmpty(Collection<E> c)
      Check whether collection c is empty.
    • isEmpty

      public static <K, E> boolean isEmpty(Map<K,E> m)
      Check whether map m is empty.
    • isEmpty

      public static boolean isEmpty(CharSequence c)
      Check whether charsequence c is empty.
    • isNotEmpty

      public static <E> boolean isNotEmpty(Collection<E> c)
      Check whether collection c is NOT empty.
    • isNotEmpty

      public static boolean isNotEmpty(CharSequence c)
      Check whether charsequence c is NOT empty.
    • isString

      public static boolean isString(Object obj)
    • isWhitespace

      public static boolean isWhitespace(String s)
      Returns true if string s is empty or whitespace characters only.
    • stripCharsInBag

      public static String stripCharsInBag(String s, String bag)
      Removes all characters which appear in string bag from string s.
    • isInteger

      public static boolean isInteger(String s)
      Returns true if all characters in string s are numbers. Accepts non-signed integers only. Does not accept floating point, exponential notation, etc.
    • isSignedInteger

      public static boolean isSignedInteger(String s)
      Returns true if all characters are numbers; first character is allowed to be + or - as well. Does not accept floating point, exponential notation, etc.
    • isSignedLong

      public static boolean isSignedLong(String s)
      Returns true if all characters are numbers; first character is allowed to be + or - as well. Does not accept floating point, exponential notation, etc.
    • isPositiveInteger

      public static boolean isPositiveInteger(String s)
      Returns true if string s is an integer > 0. NOTE: using the Java Long object for greatest precision
    • isNonnegativeInteger

      public static boolean isNonnegativeInteger(String s)
      Returns true if string s is an integer >= 0
    • isNegativeInteger

      public static boolean isNegativeInteger(String s)
      Returns true if string s is an integer < 0
    • isNonpositiveInteger

      public static boolean isNonpositiveInteger(String s)
      Returns true if string s is an integer <= 0
    • isFloat

      public static boolean isFloat(String s)
      True if string s is an unsigned floating point(real) number. Also returns true for unsigned integers. If you wish to distinguish between integers and floating point numbers, first call isInteger, then call isFloat. Does not accept exponential notation.
    • isFloat

      public static boolean isFloat(String s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal)
      General routine for testing whether a string is a float.
    • isDouble

      public static boolean isDouble(String s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal)
      General routine for testing whether a string is a double.
    • isSignedFloat

      public static boolean isSignedFloat(String s)
      True if string s is a signed or unsigned floating point (real) number. First character is allowed to be + or -. Also returns true for unsigned integers. If you wish to distinguish between integers and floating point numbers, first call isSignedInteger, then call isSignedFloat.
    • isSignedDouble

      public static boolean isSignedDouble(String s)
      True if string s is a signed or unsigned floating point (real) number. First character is allowed to be + or -. Also returns true for unsigned integers. If you wish to distinguish between integers and floating point numbers, first call isSignedInteger, then call isSignedFloat.
    • isAlphabetic

      public static boolean isAlphabetic(String s)
      Returns true if string s is letters only. NOTE: This should handle i18n version to support European characters, etc. since it now uses Character.isLetter()
    • isAlphanumeric

      public static boolean isAlphanumeric(String s)
      Returns true if string s is English letters (A .. Z, a..z) and numbers only. NOTE: Need i18n version to support European characters. This could be tricky due to different character sets and orderings for various languages and platforms.
    • isSSN

      public static boolean isSSN(String s)
      isSSN returns true if string s is a valid U.S. Social Security Number. Must be 9 digits.
    • isContiguousZipCode

      public static boolean isContiguousZipCode(String s)
      Returns true if string s is a valid contiguous U.S. Zip code. Must be 5 or 9 digits only.
    • isStateCode

      public static boolean isStateCode(String s)
      Return true if s is a valid U.S. Postal Code (abbreviation for state).
    • isContiguousStateCode

      public static boolean isContiguousStateCode(String s)
      Return true if s is a valid contiguous U.S. Postal Code (abbreviation for state).
    • isEmail

      public static boolean isEmail(String s)
    • isEmailList

      public static boolean isEmailList(String s)
      Checks a String for a valid Email-List seperated by ",".
    • isUrl

      public static boolean isUrl(String s)
      isUrl returns true if the string contains ://
      Parameters:
      s - String to validate Note: this does not handle "component://" specific to OFBiz
      Returns:
      true if s contains ://
    • urlInString

      public static boolean urlInString(String s)
      urlInString returns true if the string contains :// and does not start with "component://"
      Parameters:
      s - String to validate
      Returns:
      true if s contains :// and does not start with "component://"
    • isValidUrl

      public static boolean isValidUrl(String s)
      isValidUrl returns true if the string is a valid URL (using Commons UrlValidator)
      Parameters:
      s - String to validate
      Returns:
      true if s contains if the string is a valid URL (using Commons UrlValidator)
    • isYear

      public static boolean isYear(String s)
      isYear returns true if string s is a valid Year number. Must be 2 or 4 digits only. For Year 2000 compliance, you are advised to use 4-digit year numbers everywhere.
    • isIntegerInRange

      public static boolean isIntegerInRange(String s, int a, int b)
      isIntegerInRange returns true if string s is an integer within the range of integer arguments a and b, inclusive.
    • isMonth

      public static boolean isMonth(String s)
      isMonth returns true if string s is a valid month number between 1 and 12.
    • isDay

      public static boolean isDay(String s)
      isDay returns true if string s is a valid day number between 1 and 31.
    • daysInFebruary

      public static int daysInFebruary(int year)
      Given integer argument year, returns number of days in February of that year.
    • isHour

      public static boolean isHour(String s)
      isHour returns true if string s is a valid number between 0 and 23.
    • isMinute

      public static boolean isMinute(String s)
      isMinute returns true if string s is a valid number between 0 and 59.
    • isSecond

      public static boolean isSecond(String s)
      isSecond returns true if string s is a valid number between 0 and 59.
    • isDate

      public static boolean isDate(String year, String month, String day)
      isDate returns true if string arguments year, month, and day form a valid date.
    • isDate

      public static boolean isDate(String date)
      isDate returns true if string argument date forms a valid date.
    • isDateAfterToday

      public static boolean isDateAfterToday(String date)
      isDate returns true if string argument date forms a valid date and is after today.
    • isDateBeforeToday

      public static boolean isDateBeforeToday(String date)
      isDate returns true if string argument date forms a valid date and is before today.
    • isDateBeforeNow

      public static boolean isDateBeforeNow(Timestamp date)
    • isDateAfterNow

      public static boolean isDateAfterNow(Timestamp date)
    • isTime

      public static boolean isTime(String hour, String minute, String second)
      isTime returns true if string arguments hour, minute, and second form a valid time.
    • isTime

      public static boolean isTime(String time)
      isTime returns true if string argument time forms a valid time.
    • isValueLinkCard

      public static boolean isValueLinkCard(String stPassed)
      Check to see if a card number is a valid ValueLink Gift Card
      Parameters:
      stPassed - a string representing a valuelink gift card
      Returns:
      true, if the number passed simple checks
    • isOFBGiftCard

      public static boolean isOFBGiftCard(String stPassed)
      Check to see if a card number is a valid OFB Gift Card (Certifiicate)
      Parameters:
      stPassed - a string representing a gift card
      Returns:
      tru, if the number passed simple checks
    • isGiftCard

      public static boolean isGiftCard(String stPassed)
      Check to see if a card number is a supported Gift Card
      Parameters:
      stPassed - a string representing a gift card
      Returns:
      true, if the number passed simple checks
    • getLuhnSum

      public static int getLuhnSum(String stPassed)
    • getLuhnCheckDigit

      public static int getLuhnCheckDigit(String stPassed)
    • sumIsMod10

      public static boolean sumIsMod10(int sum)
    • appendCheckDigit

      public static String appendCheckDigit(String stPassed)
    • isCreditCard

      public static boolean isCreditCard(String stPassed)
      Checks credit card number with Luhn Mod-10 test
      Parameters:
      stPassed - a string representing a credit card number
      Returns:
      true, if the credit card number passes the Luhn Mod-10 test, false otherwise
    • isVisa

      public static boolean isVisa(String cc)
      Checks to see if the cc number is a valid Visa number
      Parameters:
      cc - a string representing a credit card number; Sample number: 4111 1111 1111 1111(16 digits)
      Returns:
      true, if the credit card number is a valid VISA number, false otherwise
    • isMasterCard

      public static boolean isMasterCard(String cc)
      Checks to see if the cc number is a valid Master Card number
      Parameters:
      cc - a string representing a credit card number; MasterCard numbers either start with the numbers 51 through 55 or with the numbers 2221 through 2720. All have 16 digits; Sample number: 5500 0000 0000 0004(16 digits)
      Returns:
      true, if the credit card number is a valid MasterCard number, false otherwise
    • isAmericanExpress

      public static boolean isAmericanExpress(String cc)
      Checks to see if the cc number is a valid American Express number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 340000000000009(15 digits)
      Returns:
      true, if the credit card number is a valid American Express number, false otherwise
    • isDinersClub

      public static boolean isDinersClub(String cc)
      Checks to see if the cc number is a valid Diners Club number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 30000000000004(14 digits)
      Returns:
      true, if the credit card number is a valid Diner's Club number, false otherwise
    • isCarteBlanche

      public static boolean isCarteBlanche(String cc)
      Checks to see if the cc number is a valid Carte Blanche number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 30000000000004(14 digits)
      Returns:
      true, if the credit card number is a valid Carte Blanche number, false otherwise
    • isDiscover

      public static boolean isDiscover(String cc)
      Checks to see if the cc number is a valid Discover number
      Parameters:
      cc - - a string representing a credit card number; Discover card numbers begin with 6011 or 65. All have 16 digits; Sample number: 6011000000000004(16 digits)
      Returns:
      true, if the credit card number is a valid Discover card number, false otherwise
    • isEnRoute

      public static boolean isEnRoute(String cc)
      Checks to see if the cc number is a valid EnRoute number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 201400000000009(15 digits)
      Returns:
      true, if the credit card number is a valid enRoute card number, false, otherwise
    • isJCB

      public static boolean isJCB(String cc)
      Checks to see if the cc number is a valid JCB number
      Parameters:
      cc - - a string representing a credit card number; JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits;Sample number: 3088000000000009(16 digits)
      Returns:
      true, if the credit card number is a valid JCB card number, false otherwise
    • isSwitch

      public static boolean isSwitch(String cc)
      Checks to see if the cc number is a valid Switch number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 6331100000000096(16 digits)
      Returns:
      true, if the credit card number is a valid Switch card number, false otherwise
    • isSolo

      public static boolean isSolo(String cc)
      Checks to see if the cc number is a valid Solo number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 6331100000000096 (16 digits)
      Returns:
      true, if the credit card number is a valid Solo card number, false otherwise
    • isVisaElectron

      public static boolean isVisaElectron(String cc)
      Checks to see if the cc number is a valid Visa Electron number
      Parameters:
      cc - - a string representing a credit card number; Sample number: 4175000000000001(16 digits)
      Returns:
      true, if the credit card number is a valid Visa Electron card number, false otherwise
    • isAnyCard

      public static boolean isAnyCard(String ccPassed)
      Checks to see if the cc number is a valid number for any accepted credit card
      Parameters:
      ccPassed - - a string representing a credit card number
      Returns:
      true, if the credit card number is any valid credit card number for any of the accepted card types, false otherwise
    • getCardType

      public static String getCardType(String ccPassed)
      Checks to see if the cc number is a valid number for any accepted credit card, and return the name of that type
      Parameters:
      ccPassed - - a string representing a credit card number
      Returns:
      true, if the credit card number is any valid credit card number for any of the accepted card types, false otherwise
    • isCardMatch

      public static boolean isCardMatch(String cardType, String cardNumberPassed)
      Checks to see if the cc number is a valid number for the specified type
      Parameters:
      cardType - - a string representing the credit card type
      cardNumberPassed - - a string representing a credit card number
      Returns:
      true, if the credit card number is valid for the particular credit card type given in "cardType", false otherwise
    • checkValidDatabaseId

      public static String checkValidDatabaseId(String fieldStr)
    • isValidDatabaseId

      public static boolean isValidDatabaseId(String fieldStr, StringBuffer errorDetails)
    • isValidPhoneNumber

      public static boolean isValidPhoneNumber(String phoneNumber, Delegator delegator)
    • isValidPhoneNumber

      public static boolean isValidPhoneNumber(String phoneNumber, String geoId, Delegator delegator)