Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class InputMask

  1. Object
  2. InputMask

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.

Constructors

public InputMask(String pattern)Creates a mask.

Methods

public int getLength()The length of fully formatted text.
public String apply(String text)Formats text against the mask.
public String unmask(String formatted)Removes the literals of the mask, leaving what the user typed.
public boolean isComplete(String formatted)Whether text fills every slot of the mask.

Inherited methods

Constructor details

InputMask

public InputMask(String pattern)
Creates a mask.

Parameters

pattern String
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 String
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 String
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 String
the text

Returns

true when the entry is complete