Class InputMask

java.lang.Object
com.codename1.ui.InputMask

public final class InputMask extends Object

Formats text as it is typed against a pattern: a phone number, a card expiry, a postal code. In the pattern 9 stands for a digit, a for a letter, * for a letter or a digit, and every other character is a literal the mask inserts itself. A backslash makes the next character literal.

TextField phone = new TextField();
phone.setInputMask(new InputMask("(999) 999-9999"));
// typing 5551234567 shows (555) 123-4567; getText() answers the formatted text
String digits = phone.getInputMask().unmask(phone.getText()); // 5551234567

Characters that do not fit the next slot are dropped, and input past the end of the pattern is ignored. A mask is immutable and may be shared between fields.

  • Constructor Details

    • InputMask

      public InputMask(String pattern)

      Creates a mask.

      Parameters
      • pattern: the pattern, for example 99/99 or (999) 999-9999
  • Method Details

    • getLength

      public int getLength()

      The length of fully formatted text.

      Returns

      the number of characters a complete entry has

    • apply

      public String apply(String text)

      Formats text against the mask. Literals already present are recognised, so formatting formatted text answers it unchanged.

      Parameters
      • text: raw or partly formatted text
      Returns

      the formatted text; it stops after the last character typed, so a literal is added only once the character after it arrives

    • unmask

      public String unmask(String formatted)

      Removes the literals of the mask, leaving what the user typed.

      Parameters
      • formatted: text formatted by this mask
      Returns

      the characters typed into the slots

    • isComplete

      public boolean isComplete(String formatted)

      Whether text fills every slot of the mask.

      Parameters
      • formatted: the text
      Returns

      true when the entry is complete