Skip to content

Instantly share code, notes, and snippets.

@vulcandragi
Last active July 13, 2025 19:05
Show Gist options
  • Select an option

  • Save vulcandragi/8581a7112e10818fc4a34107fa934e3f to your computer and use it in GitHub Desktop.

Select an option

Save vulcandragi/8581a7112e10818fc4a34107fa934e3f to your computer and use it in GitHub Desktop.
Mask for blazor input
private void ProcessMask()
{
var newValue = new StringBuilder();
var maskIndex = 0;
var valueIndex = 0;
while (valueIndex < Value?.Length)
{
if (maskIndex >= Mask?.Length)
break;
switch (Mask?[maskIndex])
{
case '0' when char.IsNumber(Value[valueIndex]):
newValue.Append(Value[valueIndex]);
valueIndex++;
break;
case '#' when char.IsLetterOrDigit(Value[valueIndex]):
newValue.Append(Value[valueIndex]);
valueIndex++;
break;
default:
{
if (Mask?[maskIndex] != '0' && Mask?[maskIndex] != '#')
{
newValue.Append(Mask?[maskIndex]);
if (Mask?[maskIndex] == Value[valueIndex])
{
valueIndex++;
}
}
break;
}
}
maskIndex++;
}
Value = newValue.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment