Determining validation rules

Validations are used to ensure that only valid data is used by the clients. For example, it may be important to your application to ensure that every user provides a valid payment card information before it could be sent to the server.

Default behavior validate only already recognized card numbers which meet the requirements. There are several ways to change the basic validation flow:

  • XML attribute app:validationRule;

  • Validation builder.

XML attributes validation rules

VGS Collect SDK allows configure validation rules that you can apply to VGSCardNumberEditText field.

  • enable: Enable default validation rules. The Unknown payment card brands are not valid.

  • acceptUnknown: The Unknown payment card brands are valid only if they have a length from 16 to 19 and the Luhn algorithm is valid.

  • disable: Disable validation rules. All type of payment card brands are valid.

Validation builder

Alternatively, validation rules can be specified by the builder instead of an xml attribute. You can configure regex, length and checksum independently for validation of all unknown brands.

VGSCardNumberEditText supports unlimited amount of PaymentCardNumberRule objects applied at the same time.

val rule : PaymentCardNumberRule = PaymentCardNumberRule.ValidationBuilder()
    .setAlgorithm(ChecksumAlgorithm.LUHN)
    .setAllowableMinLength(15)
    .setAllowableMaxLength(19)
    .build()

Configure Checksum rule

setAlgorithm(ChecksumAlgorithm, String) method allows you to setup different behavior based on the algorithm and error message which will be returned in case validation failure. Error message is optional parameter.

  • LUHN validates with Luhn algorithm;

  • ANY card number is valid if any known by VGSCollect passed;

  • NONE skip validation of checksum.

Configure length rule

setAllowableMinLength(Int, String), setAllowableMaxLength(Int, String) allow you to configure length limitations for the payment cards and error message which will be returned in case validation failure. Error message is optional parameter.

setAllowableNumberLength(Array, String) allow you to configure exact length which the payment card should match and error message which will be returned in case validation failure. Error message is optional parameter.

Configure regular expression (Regex) rule

setRegex(String, String) allows you to validate user input in scope of matching the regex pattern and error message which will be returned in case validation failure. Error message is optional parameter.

Override default validation

The setAllowToOverrideDefaultValidation method allows you to ignore all default validations inside VGS Collect SDK. Use this setting in case you have to rely only on your own validations.

Note: Pay attention to the fact that the SDK will ignore setAllowToOverrideDefaultValidation if you don't configure any additional rules. You have to mix this setting with other validation rules like Regular expression, Length or Checksum.

Apply validation rules

You can apply validation rule toVGSCardNumberEditText by calling:

  • setRule replace all rules.

  • setRules replace all rules.

  • appendRule add rule without removing default or previously applied rules.

val cardNumber = findViewById<VGSCardNumberEditText>(R.id.cardNumber)

val firstRule : PaymentCardNumberRule = PaymentCardNumberRule.ValidationBuilder()
    .setAlgorithm(ChecksumAlgorithm.LUHN, "<ALGORITHM_ERROR_MESSAGE>")
    .setAllowableMinLength(15, "<LENGTH_ERROR_MESSAGE>")
    .setAllowableMaxLength(19)
    .build()

val secondRule : PaymentCardNumberRule = PaymentCardNumberRule.ValidationBuilder()
    .setRegex("<VALIDATION_REGEX>", "<REGEX_ERROR_MESSAGE>")
    .build()

cardNumber.setRules(listOf(firstRule, secondRule))

Read error messages

FieldState object stores validation error messages in validationErrors property. You can subscribe to FieldState object updates using OnFieldStateChangeListener listener.

cardNumber.setOnFieldStateChangeListener(object : OnFieldStateChangeListener {

     override fun onStateChange(state: FieldState) {
          state.validationErrors // Handle error messages
     }
})

Last updated