
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.
Easily format numbers like CPF, CEP, CNPJ, and phone numbers dynamically in PowerFX with reusable formulas and configurations.
nfMasks =
ForAll(
[
{ Format: "CPF"; Mask: "##.###.###-##" };
{ Format: "CEP"; Mask: "##.###-###" };
{ Format: "CNPJ"; Mask: "##.###.###/####-##" };
{ Format: "CELULAR"; Mask: "(##) #####-####" }
],
Patch(ThisRecord, { MaxLenth: Len(Mask) })
);
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
)
)
);
Default: locCEP
If(
IsBlank(Self.Text),
Reset(Self) && UpdateContext({ locCEP: Blank() }),
UpdateContext({ locCEP: fnMask(Self.Text, "CEP") }) && Reset(Self)
)
MaxLength: LookUp(nfMasks, Format = "CEP", MaxLenth)
Leverage this flexible setup to enhance input formatting and streamline user interactions in your apps.