Skip to content

Commit

Permalink
Add JSON object support to mask attribute (to support regular
Browse files Browse the repository at this point in the history
expression and other advanced options).
  • Loading branch information
jepsar authored and stephanrauh committed Mar 9, 2018
1 parent d896794 commit ecc8be5
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
}

/**
* Add script to enable the input mask.
* Add script to enable the input mask. If the mask attribute starts with {@code {} the value is expected to be a
* JSON object (and can for example be used to set a regular expression: {@code {regex:'[0-9\u0600-\u06FF]*'}}).
*
* See https://github.com/RobinHerbots/Inputmask.
*
* @param context
Expand All @@ -322,9 +324,14 @@ protected void encodeMask(FacesContext context,
ResponseWriter rw) throws IOException {
if (inputText.getMask() != null && !inputText.getMask().isEmpty()) {
rw.startElement("script", inputText);
rw.writeText("Inputmask(\"", null);
rw.writeText(inputText.getMask().replace("\"", "\\\""), null);
rw.writeText("\").mask(document.getElementById(\"", null);
rw.writeText("Inputmask(", null);
if (inputText.getMask().trim().startsWith("{")) {
rw.writeText(inputText.getMask().trim(), null);
}
else {
rw.writeText(String.format("\"%s\"", inputText.getMask().replace("\"", "\\\"")), null);
}
rw.writeText(").mask(document.getElementById(\"", null);
rw.writeText(fieldId, null);
rw.writeText("\"));", null);
rw.endElement("script");
Expand Down

0 comments on commit ecc8be5

Please sign in to comment.