This class uses a Mask internally to define a pattern of placeholder characters (e.g., underscores) and fixed characters (like hyphens or parentheses). Users can input text, and StringMask will enforce the mask constraints. For example, if the mask is "(999) 999-9999" , users can only type digits where the '9' placeholders appear.
Once an input is masked, it can be retrieved as a partially-complete or fully-complete masked string. It also supports removing characters, shifting text, and retrieving the unmasked value.
This class extends BaseTextBox and internally manages an InputMask object. Users can configure the mask via StringMask and its associated MaskOptions to allow or restrict certain characters, implement special formatting, etc.
Example usage:
MaskBox maskBox = MaskBox.create("(999) 999-9999");
maskBox.appendChildTo(document.body);
By default, MaskBox calls startMasking upon construction to activate event listeners for enforcing the mask.
Upon calling startMasking , this class intercepts keyboard and clipboard events to enforce the configured mask, updating the displayed text accordingly while keeping track of a separate masked and unmasked value.
Features:
StringMaskThis class can be created via various static of(...) methods that accept different types of input elements and optional MaskOptions .
public void StringMask(String mask)
StringMask with a given mask string and default MaskOptions .
mask
the mask pattern (e.g., "(999) 999-9999" ). Cannot be null.
public void StringMask(String mask, MaskOptions options)
StringMask with a given mask string and MaskOptions .
mask
the mask pattern (e.g., "(999) 999-9999" ). Cannot be null.
options
configuration options for how masking behaves
public String getPlaceHolder()
"(999) 999-9999" , the placeholder might be "(___) ___-____" .
the placeholder string
public int getPosition()
the current position in the masked string
public String getMaskedValue()
the masked string
public StringMask clearMasking()
this StringMask instance, for chaining
public StringMask setMask(String mask)
If the new mask is not null or empty, the unmasked value is extracted from the current masked value, the mask is cleared, and then the input is remasked according to the new pattern.
mask
the new mask string
this StringMask instance, for chaining
public StringMask clearMasking(int startPosition, int endPosition)
startPosition to endPosition .
startPosition
the starting index
endPosition
the ending index
this StringMask instance, for chaining
public StringMask maskInput(String input)
input
the string to insert
this StringMask instance, for chaining
public StringMask maskInput(int startPosition, String input)
startPosition
the position at which to begin masking
input
the string to insert
this StringMask instance, for chaining
public StringMask setPosition(int position)
position
the new cursor position
this StringMask instance, for chaining
public StringMask moveLeft()
this StringMask instance, for chaining
public StringMask moveLeft(int repeat)
repeat
how many positions to move
this StringMask instance, for chaining
public StringMask moveRight()
this StringMask instance, for chaining
public StringMask moveRight(int repeat)
repeat
how many positions to move
this StringMask instance, for chaining
public StringMask deleteLeft()
this StringMask instance, for chaining
public StringMask deleteLeft(int repeat)
repeat
how many characters to delete
this StringMask instance, for chaining
public StringMask deleteLeft(int startPosition, int repeat)
startPosition
the position from which to start deleting
repeat
the number of characters to delete
this StringMask instance, for chaining
public StringMask deleteRight()
this StringMask instance, for chaining
public StringMask deleteRight(int repeat)
repeat
how many characters to delete
this StringMask instance, for chaining
public StringMask deleteRight(int startPosition, int repeat)
startPosition
the position from which to start deleting
repeat
the number of characters to delete
this StringMask instance, for chaining
public String getUnmaskedValue()
the unmasked string (user-entered data)
public String getUnmaskedValue(int startIndex)
startIndex
the start index for extraction
the unmasked substring
public String getUnmaskedValue(int startIndex, int endIndex)
startIndex
the start index (inclusive)
endIndex
the end index (exclusive)
the unmasked substring
public MaskOptions getOptions()
public StringMask withOptions(ChildHandler<StringMask, MaskOptions> handler)
handler
a ChildHandler that takes this mask and its options for further configuration
this StringMask instance, for chaining
public void MaskBox(String mask)
MaskBox with a given mask and default options.
mask
the mask pattern (cannot be null)
public void MaskBox(String mask, MaskOptions options)
MaskBox with a given mask and custom options.
mask
the mask pattern (cannot be null)
options
the custom mask options
public void MaskBox(String label, String mask)
MaskBox with a given label and mask, using default options.
label
the label for the text box
mask
the mask pattern (cannot be null)
public void MaskBox(String label, String mask, MaskOptions options)
MaskBox with a given label, mask, and custom options.
label
the label for the text box
mask
the mask pattern (cannot be null)
options
custom MaskOptions
public static MaskBox create(String mask)
MaskBox with the specified mask and default MaskOptions .
mask
the mask pattern (e.g., "(999) 999-9999" )
a new MaskBox instance
public static MaskBox create(String mask, MaskOptions options)
MaskBox with the specified mask and custom MaskOptions .
mask
the mask pattern
options
the custom mask options
a new MaskBox instance
public static MaskBox create(String label, String mask)
MaskBox with the specified label and mask, using default options.
label
the label for the text box
mask
the mask pattern
a new MaskBox instance
public static MaskBox create(String label, String mask, MaskOptions options)
MaskBox with the specified label, mask, and custom options.
label
the label for the text box
mask
the mask pattern
options
the custom mask options
a new MaskBox instance
public String getValue()
Returns the current unmasked value stored in this component.
public MaskBox setMask(String mask)
mask
the new mask pattern
this MaskBox instance
public String getUnmaskedValue()
public String getMaskedValue()
the masked string
public InputMask getInputMask()
public MaskBox withInputMask(ChildHandler<MaskBox, InputMask> handler)
handler
a lambda or method reference to be applied to the input mask
this MaskBox instance, for method chaining
public StringMask getStringMask()
public MaskBox withStringMask(ChildHandler<MaskBox, StringMask> handler)
handler
a lambda or method reference to be applied to the string mask
this MaskBox instance, for method chaining
public MaskBox withOptions(ChildHandler<MaskBox, MaskOptions> handler)
handler
a lambda or method reference to be applied to the mask options
this MaskBox instance, for method chaining
public String getType()
Specifies the input type for the underlying DOM element, which is "text" by default for masked input.
public void InputMask(InputElement inputElement, String mask)
InputMask for a Domino InputElement and basic mask pattern.
inputElement
the Domino UI wrapper for
mask
the mask pattern
public void InputMask(HTMLInputElement inputElement, String mask)
InputMask for a raw HTMLInputElement and basic mask pattern.
inputElement
the element
mask
the mask pattern
public void InputMask(TextAreaElement textAreaElement, String mask)
InputMask for a Domino TextAreaElement and basic mask pattern.
textAreaElement
the Domino UI wrapper for