Package org.apache.ofbiz.base.util
Class UtilValidate
java.lang.Object
org.apache.ofbiz.base.util.UtilValidate
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.
==============================================================================
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 Summary
Modifier and TypeFieldDescriptionstatic final String
Valid contiguous U.S.static final String
non-digit characters which are allowed in credit card numbersstatic final String
decimal point character differs by language and culturestatic final String
digit charactersstatic final int
U.S.static final int
U.S.static final int
static final int
static final int
U.S.static final int
U.S.static final String
letter charactersstatic final String
lower-case letter charactersstatic final String
non-digit characters which are allowed in phone numbersstatic final String
non-digit characters which are allowed in Social Security Numbersstatic final String
upper-case letter charactersstatic final String
Delimiter for USStateCodes Stringstatic final String
Valid U.S.static final String
characters which are allowed in Social Security Numbersstatic final String
characters which are allowed in US phone numbersstatic final String
characters which are allowed in international phone numbers(a leading + is OK)static final String
characters which are allowed in Social Security Numbersstatic final String
our preferred delimiter for reformatting ZIP Codesstatic final String
non-digit characters which are allowed in ZIP Codes -
Method Summary
Modifier and TypeMethodDescriptionstatic String
appendCheckDigit
(String stPassed) static String
checkValidDatabaseId
(String fieldStr) static int
daysInFebruary
(int year) Given integer argument year, returns number of days in February of that year.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 typestatic int
getLuhnCheckDigit
(String stPassed) static int
getLuhnSum
(String stPassed) static boolean
Returns true if string s is letters only.static boolean
Returns true if string s is English letters (A ..static boolean
Checks to see if the cc number is a valid American Express numberstatic boolean
Checks to see if the cc number is a valid number for any accepted credit cardstatic boolean
isCardMatch
(String cardType, String cardNumberPassed) Checks to see if the cc number is a valid number for the specified typestatic boolean
isCarteBlanche
(String cc) Checks to see if the cc number is a valid Carte Blanche numberstatic boolean
Return true if s is a valid contiguous U.S.static boolean
Returns true if string s is a valid contiguous U.S.static boolean
isCreditCard
(String stPassed) Checks credit card number with Luhn Mod-10 teststatic boolean
isDate returns true if string argument date forms a valid date.static boolean
isDate returns true if string arguments year, month, and day form a valid date.static boolean
isDateAfterNow
(Timestamp date) static boolean
isDateAfterToday
(String date) isDate returns true if string argument date forms a valid date and is after today.static boolean
isDateBeforeNow
(Timestamp date) static boolean
isDateBeforeToday
(String date) isDate returns true if string argument date forms a valid date and is before today.static boolean
isDay returns true if string s is a valid day number between 1 and 31.static boolean
isDinersClub
(String cc) Checks to see if the cc number is a valid Diners Club numberstatic boolean
isDiscover
(String cc) Checks to see if the cc number is a valid Discover numberstatic boolean
General routine for testing whether a string is a double.static boolean
static boolean
Checks a String for a valid Email-List seperated by ",".static boolean
Check whether charsequence c is empty.static boolean
Check whether an object is empty, will see if it is a String, Map, Collection, etc.static <E> boolean
isEmpty
(Collection<E> c) Check whether collection c is empty.static <K,
E> boolean Check whether map m is empty.static boolean
Check whether IsEmpty o is empty.static boolean
Checks to see if the cc number is a valid EnRoute numberstatic boolean
True if string s is an unsigned floating point(real) number.static boolean
General routine for testing whether a string is a float.static boolean
isGiftCard
(String stPassed) Check to see if a card number is a supported Gift Cardstatic boolean
isHour returns true if string s is a valid number between 0 and 23.static boolean
Returns true if all characters in string s are numbers.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.static boolean
Checks to see if the cc number is a valid JCB numberstatic boolean
isMasterCard
(String cc) Checks to see if the cc number is a valid Master Card numberstatic boolean
isMinute returns true if string s is a valid number between 0 and 59.static boolean
isMonth returns true if string s is a valid month number between 1 and 12.static boolean
Returns true if string s is an integer < 0static boolean
Returns true if string s is an integer >= 0static boolean
Returns true if string s is an integer <= 0static boolean
Check whether charsequence c is NOT empty.static boolean
isNotEmpty
(Object o) Check whether an object is NOT empty, will see if it is a String, Map, Collection, etc.static <E> boolean
isNotEmpty
(Collection<E> c) Check whether collection c is NOT empty.static boolean
Check whether IsEmpty o is NOT empty.static boolean
isOFBGiftCard
(String stPassed) Check to see if a card number is a valid OFB Gift Card (Certifiicate)static boolean
Returns true if string s is an integer > 0.static boolean
isSecond returns true if string s is a valid number between 0 and 59.static boolean
True if string s is a signed or unsigned floating point (real) number.static boolean
True if string s is a signed or unsigned floating point (real) number.static boolean
Returns true if all characters are numbers; first character is allowed to be + or - as well.static boolean
Returns true if all characters are numbers; first character is allowed to be + or - as well.static boolean
Checks to see if the cc number is a valid Solo numberstatic boolean
isSSN returns true if string s is a valid U.S.static boolean
Return true if s is a valid U.S.static boolean
static boolean
Checks to see if the cc number is a valid Switch numberstatic boolean
isTime returns true if string argument time forms a valid time.static boolean
isTime returns true if string arguments hour, minute, and second form a valid time.static boolean
isUrl returns true if the string contains ://static boolean
isValidDatabaseId
(String fieldStr, StringBuffer errorDetails) static boolean
isValidPhoneNumber
(String phoneNumber, String geoId, Delegator delegator) static boolean
isValidPhoneNumber
(String phoneNumber, Delegator delegator) static boolean
isValidUrl
(String s) isValidUrl returns true if the string is a valid URL (using Commons UrlValidator)static boolean
isValueLinkCard
(String stPassed) Check to see if a card number is a valid ValueLink Gift Cardstatic boolean
Checks to see if the cc number is a valid Visa numberstatic boolean
isVisaElectron
(String cc) Checks to see if the cc number is a valid Visa Electron numberstatic boolean
Returns true if string s is empty or whitespace characters only.static boolean
isYear returns true if string s is a valid Year number.static String
stripCharsInBag
(String s, String bag) Removes all characters which appear in string bag from string s.static boolean
sumIsMod10
(int sum) static boolean
urlInString returns true if the string contains :// and does not start with "component://"
-
Field Details
-
DIGITS
digit characters- See Also:
-
LOWER_CASE_LETTERS
lower-case letter characters- See Also:
-
UPPER_CASE_LETTERS
upper-case letter characters- See Also:
-
LETTERS
letter characters- See Also:
-
DECIMAL_POINT_DELIMITER
decimal point character differs by language and culture- See Also:
-
PHONE_NUMBER_DELIMITERS
non-digit characters which are allowed in phone numbers- See Also:
-
VALID_US_PHONE_CHARS
characters which are allowed in US phone numbers- See Also:
-
VALID_WORLD_PHONE_CHARS
characters which are allowed in international phone numbers(a leading + is OK)- See Also:
-
SSN_DELIMITERS
non-digit characters which are allowed in Social Security Numbers- See Also:
-
VALID_SSN_CHARS
characters which are allowed in Social Security Numbers- See Also:
-
DIGITS_IN_SSN
public static final int DIGITS_IN_SSNU.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_PHONEU.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
non-digit characters which are allowed in ZIP Codes- See Also:
-
ZIP_CODE_DELIMITER
our preferred delimiter for reformatting ZIP Codes- See Also:
-
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_1U.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_2U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789.- See Also:
-
CREDIT_CARD_DELIMITERS
non-digit characters which are allowed in credit card numbers- See Also:
-
US_STATE_CODE_DELIMITER
Delimiter for USStateCodes String- See Also:
-
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
Valid contiguous U.S. postal codes- See Also:
-
-
Method Details
-
isEmpty
Check whether an object is empty, will see if it is a String, Map, Collection, etc. -
isNotEmpty
Check whether an object is NOT empty, will see if it is a String, Map, Collection, etc. -
isEmpty
Check whether IsEmpty o is empty. -
isNotEmpty
Check whether IsEmpty o is NOT empty. -
isEmpty
Check whether collection c is empty. -
isEmpty
Check whether map m is empty. -
isEmpty
Check whether charsequence c is empty. -
isNotEmpty
Check whether collection c is NOT empty. -
isNotEmpty
Check whether charsequence c is NOT empty. -
isString
-
isWhitespace
Returns true if string s is empty or whitespace characters only. -
stripCharsInBag
Removes all characters which appear in string bag from string s. -
isInteger
Returns true if all characters in string s are numbers. Accepts non-signed integers only. Does not accept floating point, exponential notation, etc. -
isSignedInteger
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
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
Returns true if string s is an integer > 0. NOTE: using the Java Long object for greatest precision -
isNonnegativeInteger
Returns true if string s is an integer >= 0 -
isNegativeInteger
Returns true if string s is an integer < 0 -
isNonpositiveInteger
Returns true if string s is an integer <= 0 -
isFloat
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
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
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
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
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
isSSN returns true if string s is a valid U.S. Social Security Number. Must be 9 digits. -
isContiguousZipCode
Returns true if string s is a valid contiguous U.S. Zip code. Must be 5 or 9 digits only. -
isStateCode
Return true if s is a valid U.S. Postal Code (abbreviation for state). -
isContiguousStateCode
Return true if s is a valid contiguous U.S. Postal Code (abbreviation for state). -
isEmail
-
isEmailList
Checks a String for a valid Email-List seperated by ",". -
isUrl
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
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
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
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
isIntegerInRange returns true if string s is an integer within the range of integer arguments a and b, inclusive. -
isMonth
isMonth returns true if string s is a valid month number between 1 and 12. -
isDay
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
isHour returns true if string s is a valid number between 0 and 23. -
isMinute
isMinute returns true if string s is a valid number between 0 and 59. -
isSecond
isSecond returns true if string s is a valid number between 0 and 59. -
isDate
isDate returns true if string arguments year, month, and day form a valid date. -
isDate
isDate returns true if string argument date forms a valid date. -
isDateAfterToday
isDate returns true if string argument date forms a valid date and is after today. -
isDateBeforeToday
isDate returns true if string argument date forms a valid date and is before today. -
isDateBeforeNow
-
isDateAfterNow
-
isTime
isTime returns true if string arguments hour, minute, and second form a valid time. -
isTime
isTime returns true if string argument time forms a valid time. -
isValueLinkCard
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
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
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
-
getLuhnCheckDigit
-
sumIsMod10
public static boolean sumIsMod10(int sum) -
appendCheckDigit
-
isCreditCard
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Checks to see if the cc number is a valid number for the specified type- Parameters:
cardType
- - a string representing the credit card typecardNumberPassed
- - 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
-
isValidDatabaseId
-
isValidPhoneNumber
-
isValidPhoneNumber
-