# 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 <a href="#xml-attributes-validation-rules" id="xml-attributes-validation-rules"></a>

**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 <a href="#validation-builder" id="validation-builder"></a>

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.

{% tabs %}
{% tab title="Kotlin" %}

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

{% endtab %}

{% tab title="Java" %}

```java
PaymentCardNumberRule rule = new PaymentCardNumberRule.ValidationBuilder()
    .setAlgorithm(ChecksumAlgorithm.LUHN)
    .setAllowableMinLength(15)
    .setAllowableMaxLength(19)
    .build();
```

{% endtab %}
{% endtabs %}

#### Configure Checksum rule <a href="#configure-checksum-rule" id="configure-checksum-rule"></a>

`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 <a href="#configure-length-rule" id="configure-length-rule"></a>

`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 <a href="#configure-regular-expression-regex-rule" id="configure-regular-expression-regex-rule"></a>

`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 <a href="#override-default-validation" id="override-default-validation"></a>

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 <a href="#apply-validation-rules" id="apply-validation-rules"></a>

You can apply validation rule to`VGSCardNumberEditText` by calling:

* **setRule** replace all rules.
* **setRules** replace all rules.
* **appendRule** add rule without removing default or previously applied rules.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
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))
```

{% endtab %}

{% tab title="Java" %}

```java
VGSCardNumberEditText cardNumber = findViewById(R.id.cardNumber);

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

PaymentCardNumberRule secondRule = new PaymentCardNumberRule.ValidationBuilder()
    .setRegex("<VALIDATION_REGEX>", "<REGEX_ERROR_MESSAGE>")
    .build();

cardNumber.setRules(Arrays.asList(firstRule, secondRule));
```

{% endtab %}
{% endtabs %}

#### Read error messages <a href="#read-error-messages" id="read-error-messages"></a>

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

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
cardNumber.setOnFieldStateChangeListener(object : OnFieldStateChangeListener {

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

{% endtab %}

{% tab title="Java" %}

```java
cardNumber.setOnFieldStateChangeListener(new OnFieldStateChangeListener() {

     @Override
     public void onStateChange(@NonNull FieldState state) {
          state.getValidationErrors(); // Handle error messages
     }
});
```

{% endtab %}
{% endtabs %}
