Package org.ofbiz.core.util
Class UtilValidate
java.lang.Object
org.ofbiz.core.util.UtilValidate
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringValid contiguous U.S.static final Stringnon-digit characters which are allowed in credit card numbersstatic final int[]An array of ints representing the number of days in each month of the year.static final Stringdecimal point character differs by language and culturestatic final booleanboolean specifying by default whether or not it is okay for a String to be emptystatic final Stringdigit charactersstatic final intU.S.static final intstatic final intstatic final intU.S.static final intU.S.static final intU.S.static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringletter charactersstatic final Stringlower-case letter charactersstatic final Stringnon-digit characters which are allowed in phone numbersstatic final Stringnon-digit characters which are allowed in Social Security Numbersstatic final Stringupper-case letter charactersstatic final StringDelimiter for USStateCodes Stringstatic final StringValid U.S.static final Stringcharacters which are allowed in Social Security Numbersstatic final Stringcharacters which are allowed in US phone numbersstatic final Stringcharacters which are allowed in international phone numbers(a leading + is OK)static final Stringcharacters which are allowed in Social Security Numbersstatic final Stringwhitespace charactersstatic final Stringour preferred delimiter for reformatting ZIP Codesstatic final Stringnon-digit characters which are allowed in ZIP Codes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanstatic booleancharInString(char c, String s) Returns true if single character c(actually a string) is contained within string s.static intdaysInFebruary(int year) Given integer argument year, returns number of days in February of that year.static StringgetCardType(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 booleanReturns true if string s is letters only.static booleanReturns true if string s is English letters (A ..static booleanChecks to see if the cc number is a valid American Express numberstatic booleanChecks to see if the cc number is a valid number for any accepted credit cardstatic booleanisCardMatch(String cardType, String cardNumberPassed) Checks to see if the cc number is a valid number for the specified typestatic booleanisCarteBlanche(String cc) Checks to see if the cc number is a valid Carte Blanche numberstatic booleanReturn true if s is a valid contiguous U.S.static booleanReturns true if string s is a valid contiguous U.S.static booleanisCreditCard(String stPassed) Checks credit card number with Luhn Mod-10 teststatic booleanisDate returns true if string argument date forms a valid date.static booleanisDate returns true if string arguments year, month, and day form a valid date.static booleanisDateAfterToday(String date) isDate returns true if string argument date forms a valid date and is after today.static booleanisDay returns true if string s is a valid day number between 1 and 31.static booleanisDigit(char c) Returns true if character c is a digit (0 ..static booleanisDinersClub(String cc) Checks to see if the cc number is a valid Diners Club numberstatic booleanisDiscover(String cc) Checks to see if the cc number is a valid Discover numberstatic booleanEmail 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 .static booleanCheck whether string s is empty.static booleanisEmpty(Collection<?> c) Check whether collection c is empty.static booleanChecks to see if the cc number is a valid EnRoute numberstatic booleanTrue if string s is an unsigned floating point(real) number.static booleanisHour returns true if string s is a valid number between 0 and 23.static booleanReturns true if all characters in string s are numbers.static booleanisIntegerInRange(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 booleanisInternationalPhoneNumber returns true if string s is a valid international phone number.static booleanChecks to see if the cc number is a valid JCB numberstatic booleanisLetter(char c) Returns true if character c is an English letter (A ..static booleanisLetterOrDigit(char c) Returns true if character c is a letter or digit.static booleanisMasterCard(String cc) Checks to see if the cc number is a valid Master Card numberstatic booleanisMinute returns true if string s is a valid number between 0 and 59.static booleanisMonth returns true if string s is a valid month number between 1 and 12.static booleanReturns true if string s is an integer < 0.static booleanReturns true if string s is an integer >= 0.static booleanReturns true if string s is an integer <= 0.static booleanisNotEmpty(String s) Check whether string s is NOT empty.static booleanisNotEmpty(Collection<?> c) Check whether collection c is NOT empty.static booleanisNotPoBox(String s) isNotPoBox returns true if address argument does not contain anything that looks like a a PO Box.static booleanReturns true if string s is an integer > 0.static booleanisSecond returns true if string s is a valid number between 0 and 59.static booleanTrue if string s is a signed or unsigned floating point (real) number.static booleanTrue if string s is a signed or unsigned floating point (real) number.static booleanReturns true if all characters are numbers; first character is allowed to be + or - as well.static booleanReturns true if all characters are numbers; first character is allowed to be + or - as well.static booleanisSSN returns true if string s is a valid U.S.static booleanReturn true if s is a valid U.S.static booleanisTime returns true if string argument time forms a valid time.static booleanisTime returns true if string arguments hour, minute, and second form a valid time.static booleanisUSPhoneAreaCode returns true if string s is a valid U.S.static booleanisUSPhoneMainNumber returns true if string s is a valid U.S.static booleanisUSPhoneNumber returns true if string s is a valid U.S.static booleanChecks to see if the cc number is a valid Visa numberstatic booleanReturns true if string s is empty or whitespace characters only.static booleanisYear returns true if string s is a valid Year number.static booleanisZIPCode returns true if string s is a valid U.S.static StringstripCharsInBag(String s, String bag) Removes all characters which appear in string bag from string s.static StringstripCharsNotInBag(String s, String bag) Removes all characters which do NOT appear in string bag from string s.static StringRemoves initial(leading) whitespace characters from s.static StringRemoves all whitespace characters from s.
-
Field Details
-
defaultEmptyOK
public static final boolean defaultEmptyOKboolean specifying by default whether or not it is okay for a String to be empty- See Also:
-
digits
digit characters- See Also:
-
lowercaseLetters
lower-case letter characters- See Also:
-
uppercaseLetters
upper-case letter characters- See Also:
-
letters
letter characters- See Also:
-
whitespace
whitespace characters- See Also:
-
decimalPointDelimiter
decimal point character differs by language and culture- See Also:
-
phoneNumberDelimiters
non-digit characters which are allowed in phone numbers- See Also:
-
validUSPhoneChars
characters which are allowed in US phone numbers- See Also:
-
validWorldPhoneChars
characters which are allowed in international phone numbers(a leading + is OK)- See Also:
-
SSNDelimiters
non-digit characters which are allowed in Social Security Numbers- See Also:
-
validSSNChars
characters which are allowed in Social Security Numbers- See Also:
-
digitsInSocialSecurityNumber
public static final int digitsInSocialSecurityNumberU.S. Social Security Numbers have 9 digits. They are formatted as 123-45-6789.- See Also:
-
digitsInUSPhoneNumber
public static final int digitsInUSPhoneNumberU.S. phone numbers have 10 digits. They are formatted as 123 456 7890 or(123) 456-7890.- See Also:
-
digitsInUSPhoneAreaCode
public static final int digitsInUSPhoneAreaCode- See Also:
-
digitsInUSPhoneMainNumber
public static final int digitsInUSPhoneMainNumber- See Also:
-
ZipCodeDelimiters
non-digit characters which are allowed in ZIP Codes- See Also:
-
ZipCodeDelimeter
our preferred delimiter for reformatting ZIP Codes- See Also:
-
validZipCodeChars
characters which are allowed in Social Security Numbers- See Also:
-
digitsInZipCode1
public static final int digitsInZipCode1U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789.- See Also:
-
digitsInZipCode2
public static final int digitsInZipCode2U.S. ZIP codes have 5 or 9 digits. They are formatted as 12345 or 12345-6789.- See Also:
-
creditCardDelimiters
non-digit characters which are allowed in credit card numbers- See Also:
-
isNotEmptyMsg
- See Also:
-
isStateCodeMsg
- See Also:
-
isContiguousStateCodeMsg
- See Also:
-
isZipCodeMsg
- See Also:
-
isUSPhoneMsg
- See Also:
-
isUSPhoneAreaCodeMsg
- See Also:
-
isUSPhoneMainNumberMsg
- See Also:
-
isContiguousZipCodeMsg
- See Also:
-
isInternationalPhoneNumberMsg
- See Also:
-
isSSNMsg
- See Also:
-
isEmailMsg
- See Also:
-
isAnyCardMsg
- See Also:
-
isCreditCardPrefixMsg
- See Also:
-
isCreditCardSuffixMsg
- See Also:
-
isDayMsg
- See Also:
-
isMonthMsg
- See Also:
-
isYearMsg
- See Also:
-
isDatePrefixMsg
- See Also:
-
isDateSuffixMsg
- See Also:
-
isHourMsg
- See Also:
-
isMinuteMsg
- See Also:
-
isSecondMsg
- See Also:
-
isTimeMsg
- See Also:
-
isDateMsg
- See Also:
-
isDateAfterToday
- See Also:
-
isIntegerMsg
- See Also:
-
isSignedIntegerMsg
- See Also:
-
isLongMsg
- See Also:
-
isSignedLongMsg
- See Also:
-
isFloatMsg
- See Also:
-
isSignedFloatMsg
- See Also:
-
isSignedDoubleMsg
- See Also:
-
daysInMonth
public static final int[] daysInMonthAn array of ints representing the number of days in each month of the year. Note: February varies depending on the year -
USStateCodeDelimiter
Delimiter for USStateCodes String- See Also:
-
USStateCodes
Valid U.S. Postal Codes for states, territories, armed forces, etc. See http://www.usps.gov/ncsc/lookups/abbr_state.txt.- See Also:
-
ContiguousUSStateCodes
Valid contiguous U.S. postal codes- See Also:
-
-
Constructor Details
-
UtilValidate
public UtilValidate()
-
-
Method Details
-
areEqual
-
isEmpty
Check whether string s is empty. -
isEmpty
Check whether collection c is empty. -
isNotEmpty
Check whether string s is NOT empty. -
isNotEmpty
Check whether collection c is NOT empty. -
isWhitespace
Returns true if string s is empty or whitespace characters only. -
stripCharsInBag
Removes all characters which appear in string bag from string s. -
stripCharsNotInBag
Removes all characters which do NOT appear in string bag from string s. -
stripWhitespace
Removes all whitespace characters from s. Member whitespace(see above) defines which characters are considered whitespace. -
charInString
Returns true if single character c(actually a string) is contained within string s. -
stripInitialWhitespace
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
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. -
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. -
isUSPhoneNumber
isUSPhoneNumber returns true if string s is a valid U.S. Phone Number. Must be 10 digits. -
isUSPhoneAreaCode
isUSPhoneAreaCode returns true if string s is a valid U.S. Phone Area Code. Must be 3 digits. -
isUSPhoneMainNumber
isUSPhoneMainNumber returns true if string s is a valid U.S. Phone Main Number. Must be 7 digits. -
isInternationalPhoneNumber
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
isZIPCode returns true if string s is a valid U.S. ZIP code. Must be 5 or 9 digits only. -
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
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
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. -
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. -
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; 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; 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; Sample number: 3088000000000009(16 digits)- Returns:
- true, if the credit card number is a valid JCB 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
-
isNotPoBox
isNotPoBox returns true if address argument does not contain anything that looks like a a PO Box.
-