Språk

SaveInfo.Builder.SetValidator(IValidator) Method

Definition

Sets an object used to validate the user input - if the input is not valid, the autofill save UI is not shown.

[Android.Runtime.Register("setValidator", "(Landroid/service/autofill/Validator;)Landroid/service/autofill/SaveInfo$Builder;", "", ApiSince=27)]
public Android.Service.Autofill.SaveInfo.Builder SetValidator(Android.Service.Autofill.IValidator validator);
[<Android.Runtime.Register("setValidator", "(Landroid/service/autofill/Validator;)Landroid/service/autofill/SaveInfo$Builder;", "", ApiSince=27)>]
member this.SetValidator : Android.Service.Autofill.IValidator -> Android.Service.Autofill.SaveInfo.Builder

Parameters

validator
IValidator

an implementation provided by the Android System. This value cannot be null.

Returns

this builder. This value cannot be null.

Attributes

Remarks

Sets an object used to validate the user input - if the input is not valid, the autofill save UI is not shown.

Typically used to validate credit card numbers. Examples:

Validator for a credit number that must have exactly 16 digits:

Validator validator = new RegexValidator(ccNumberId, Pattern.compile(""^\\d{16}$"))

Validator for a credit number that must pass a Luhn checksum and either have 16 digits, or 15 digits starting with 108:

import static android.service.autofill.Validators.and;
import static android.service.autofill.Validators.or;

Validator validator =
  and(
    new LuhnChecksumValidator(ccNumberId),
    or(
      new RegexValidator(ccNumberId, Pattern.compile("^\\d{16}$")),
      new RegexValidator(ccNumberId, Pattern.compile("^108\\d{12}$"))
    )
  );

Note: the example above is just for illustrative purposes; the same validator could be created using a single regex for the OR part:

Validator validator =
  and(
    new LuhnChecksumValidator(ccNumberId),
    new RegexValidator(ccNumberId, Pattern.compile(""^(\\d{16}|108\\d{12})$"))
  );

Validator for a credit number contained in just 4 fields and that must have exactly 4 digits on each field:

import static android.service.autofill.Validators.and;

Validator validator =
  and(
    new RegexValidator(ccNumberId1, Pattern.compile("^\\d{4}$")),
    new RegexValidator(ccNumberId2, Pattern.compile("^\\d{4}$")),
    new RegexValidator(ccNumberId3, Pattern.compile("^\\d{4}$")),
    new RegexValidator(ccNumberId4, Pattern.compile("^\\d{4}$"))
  );

Android reference for android.service.autofill.SaveInfo.Builder.setValidator.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to