Class UtilValidate

java.lang.Object
org.ofbiz.core.util.UtilValidate

public class UtilValidate extends Object

General input/data validation methods

Utility methods for validating data, especially input. See detailed description below. Created May 21, 2001

Version:
1.0
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.
================================================================
Author:
David E. Jones
  • Field Details

  • Constructor Details

    • UtilValidate

      public UtilValidate()
  • Method Details

    • areEqual

      public static boolean areEqual(Object obj, Object obj2)
    • isEmpty

      public static boolean isEmpty(String s)
      Check whether string s is empty.
    • isEmpty

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

      public static boolean isNotEmpty(String s)
      Check whether string s is NOT empty.
    • isNotEmpty

      public static boolean isNotEmpty(Collection<?> c)
      Check whether collection c is NOT empty.
    • 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.
    • stripCharsNotInBag

      public static String stripCharsNotInBag(String s, String bag)
      Removes all characters which do NOT appear in string bag from string s.
    • stripWhitespace

      public static String stripWhitespace(String s)
      Removes all whitespace characters from s. Member whitespace(see above) defines which characters are considered whitespace.
    • charInString

      public static boolean charInString(char c, String s)
      Returns true if single character c(actually a string) is contained within string s.
    • stripInitialWhitespace

      public static String stripInitialWhitespace(String s)
      Removes initial(leading) whitespace characters from s. Member whitespace(see above) defines which characters are considered whitespace.
    • isLetter

      public static boolean isLetter(char c)
      Returns true if character c is an English letter (A .. Z, a..z). NOTE: Need i18n version to support European characters. This could be tricky due to different character sets and orderings for various languages and platforms.
    • isDigit

      public static boolean isDigit(char c)
      Returns true if character c is a digit (0 .. 9).
    • isLetterOrDigit

      public static boolean isLetterOrDigit(char c)
      Returns true if character c is a letter or digit.
    • 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.
    • 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.
    • isUSPhoneNumber

      public static boolean isUSPhoneNumber(String s)
      isUSPhoneNumber returns true if string s is a valid U.S. Phone Number. Must be 10 digits.
    • isUSPhoneAreaCode

      public static boolean isUSPhoneAreaCode(String s)
      isUSPhoneAreaCode returns true if string s is a valid U.S. Phone Area Code. Must be 3 digits.
    • isUSPhoneMainNumber

      public static boolean isUSPhoneMainNumber(String s)
      isUSPhoneMainNumber returns true if string s is a valid U.S. Phone Main Number. Must be 7 digits.
    • isInternationalPhoneNumber

      public static boolean isInternationalPhoneNumber(String s)
      isInternationalPhoneNumber returns true if string s is a valid international phone number. Must be digits only; any length OK. May be prefixed by + character.
    • isZipCode

      public static boolean isZipCode(String s)
      isZIPCode returns true if string s is a valid U.S. ZIP code. Must be 5 or 9 digits only.
    • 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)
      Email address must be of form a@b.c -- in other words: - there must be at least one character before the @ - there must be at least one character before and after the . - the characters @ and . are both required
    • 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.
    • 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.
    • 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; 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; 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; Sample number: 3088000000000009(16 digits)
      Returns:
      true, if the credit card number is a valid JCB 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
    • isNotPoBox

      public static boolean isNotPoBox(String s)
      isNotPoBox returns true if address argument does not contain anything that looks like a a PO Box.