Last active
July 13, 2025 19:05
-
-
Save vulcandragi/8581a7112e10818fc4a34107fa934e3f to your computer and use it in GitHub Desktop.
Mask for blazor input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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