question: public static String validateCreditCardNumber ( String creditCardNumber , String creditCardType ) { if ( creditCardNumber == null || creditCardType == null ) return "CreditCardNumberError" ; String ccStartList = "" ; String ccLengthList = "" ; if ( creditCardType . equals ( X_C_Payment . CREDITCARDTYPE_MasterCard ) ) { ccStartList = "51,52,53,54,55" ; ccLengthList = "16" ; } else if ( creditCardType . equals ( X_C_Payment . CREDITCARDTYPE_Visa ) ) { ccStartList = "4" ; ccLengthList = "13,16" ; } else if ( creditCardType . equals ( X_C_Payment . CREDITCARDTYPE_Amex ) ) { ccStartList = "34,37" ; ccLengthList = "15" ; } else if ( creditCardType . equals ( X_C_Payment . CREDITCARDTYPE_Discover ) ) { ccStartList = "6011" ; ccLengthList = "16" ; } else if ( creditCardType . equals ( X_C_Payment . CREDITCARDTYPE_Diners ) ) { ccStartList = "300,301,302,303,304,305,36,38" ; ccLengthList = "14" ; } else { ccStartList = "2014,2149" ; ccLengthList = "15" ; ccStartList += ",3088,3096,3112,3158,3337,3528" ; ccLengthList += ",16" ; ccStartList += ",2131,1800" ; ccLengthList += ",15" ; } String ccNumber = checkNumeric ( creditCardNumber ) ; int ccLength = ccNumber . length ( ) ; boolean ccLengthOK = false ; StringTokenizer st = new StringTokenizer ( ccLengthList , "," , false ) ; while ( st . hasMoreTokens ( ) && ! ccLengthOK ) { int l = Integer . parseInt ( st . nextToken ( ) ) ; if ( ccLength == l ) ccLengthOK = true ; } if ( ! ccLengthOK ) { s_log . fine ( "validateCreditCardNumber Length=" + ccLength + " <> " + ccLengthList ) ; return "CreditCardNumberError" ; } boolean ccIdentified = false ; st = new StringTokenizer ( ccStartList , "," , false ) ; while ( st . hasMoreTokens ( ) && ! ccIdentified ) { if ( ccNumber . startsWith ( st . nextToken ( ) ) ) ccIdentified = true ; } if ( ! ccIdentified ) s_log . fine ( "validateCreditCardNumber Type=" + creditCardType + " <> " + ccStartList ) ; String check = validateCreditCardNumber ( ccNumber ) ; if ( check . length ( ) != 0 ) return check ; if ( ! ccIdentified ) return "CreditCardNumberProblem?" ; return "" ; }
target:  Validate Credit Card Number. - Check Card Type and Length