Any Number Mask

A PowerFX snippet for dynamically formatting input fields like CPF, CEP, CNPJ, and phone numbers in Power Apps. It simplifies user interactions and enhances input validation with reusable number masks.
Format Numbers in Any Mask with a Single Function!
Easily format numbers like CPF, CEP, CNPJ, and phone numbers dynamically in PowerFX with reusable formulas and configurations.
FORMULAS
Define Masks
nfMasks =
ForAll(
[
{ Format: "CPF"; Mask: "##.###.###-##" };
{ Format: "CEP"; Mask: "##.###-###" };
{ Format: "CNPJ"; Mask: "##.###.###/####-##" };
{ Format: "CELULAR"; Mask: "(##) #####-####" }
],
Patch(ThisRecord, { MaxLenth: Len(Mask) })
);
Mask Function
fnMask(Text: Text; format: Text): Text =
With(
{
_InputText: Text;
_Mask: LookUp(nfMasks As Mask, Mask.Format = format, Mask.Mask)
},
With(
{
_TableMask: Split(_Mask, "");
_TableNumbers: MatchAll(_InputText, Match.Digit)
},
Concat(
ForAll(
Sequence(CountRows(_TableMask)),
With(
{ _Caracter: Index(_TableMask, Value).Value },
If(
_Caracter = "#",
IfError(
Index(_TableNumbers, CountRows(MatchAll(Left(_Mask, Value), "#"))).FullMatch,
"0"
),
_Caracter
)
)
),
Value
)
)
);
INPUT CONTROL
Default Property
Default: locCEP
OnChange Property
If(
IsBlank(Self.Text),
Reset(Self) && UpdateContext({ locCEP: Blank() }),
UpdateContext({ locCEP: fnMask(Self.Text, "CEP") }) && Reset(Self)
)
MaxLength Property
MaxLength: LookUp(nfMasks, Format = "CEP", MaxLenth)
Leverage this flexible setup to enhance input formatting and streamline user interactions in your apps.
More Snippets from this Author
Page 1 of 0
Loading...
Loading...