Learn R Programming

assertive.data (version 0.0-3)

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, na_ignore = FALSE,
  severity = getOption("assertive.severity", "stop"))

assert_any_are_credit_card_numbers(x, na_ignore = FALSE, severity = getOption("assertive.severity", "stop"))

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

Arguments

x

Input to check.

na_ignore

A logical value. If FALSE, NA values cause an error; otherwise they do not. Like na.rm in many stats package functions, except that the position of the failing values does not change.

severity

How severe should the consequences of the assertion be? Either "stop", "warning", "message", or "none".

type

Type of credit card. Multiple types can be selected.

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 were from PayPal developer documentation. The URL is no longer available.

Examples

Run this code
# NOT RUN {
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)
assertive.base::dont_stop(assert_all_are_credit_card_numbers(x))
# }

Run the code above in your browser using DataLab