Learn R Programming

assertive (version 0.2-1)

assert_all_are_credit_card_numbers: Does the character vector contain credit card numbers?

Description

Checks that the input contains credit card numbers.

Usage

assert_all_are_credit_card_numbers(x)

assert_any_are_credit_card_numbers(x)

is_credit_card_number(x, type = c("visa", "mastercard", "amex", "diners",
  "discover", "jcb"), .xname = get_name_in_parent(x))

Arguments

x
Input to check.
type
Type of credit card. Multiple types can be selected.
.xname
Not intended to be called directly.

Value

  • A logical vector that is TRUE when the input contains valid credit card numbers.

References

http://www.regular-expressions.info/creditcard.html contains the regexes used by this function. The example valid card numbers are from http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm

Examples

Run this code
x <- c(
  #visa
  "4111 1111 1111 1111",    #spaces are allowed where they
                            #would occur on the card
  "4012888888881881",       #though they can be omitted
  "4111 1111 1111 11111",   #too many digits
  "4012888888881882",       #bad check digit
  #mastercard
  "5555 5555 5555 4444",
  "5105 1051 0510 5100",
  "5655 5555 5555 4443",    #starts 56
  "51051 051 0510 5100",    #bad spacing
  #amex
  "3782 822463 10005",
  "3714 496353 98431",
  "3787 344936 71000",
  "3782 822463 1005",       #not enough digits
  #diners
  "3056 930902 5904",
  "3852 000002 3237",
  #discover
  "6011 1111 1111 1117",
  "6011 0009 9013 9424",
  #jcb
  "3530 1113 3330 0000",
  "3566 0020 2036 0505"
)
is_credit_card_number(x)
assert_any_are_credit_card_numbers(x)
dont_stop(assert_all_are_credit_card_numbers(x))

Run the code above in your browser using DataLab