Articles in this section
Category / Section

How to limit the number of Inputs in .NET MAUI Masked Entry (SfMaskedEntry)?

This section demonstrates how to restrict the number of inputs in the .NET MAUI Masked Entry (SfMaskedEntry) control while utilizing the TextInputLayout. By setting the CharMaxLength property in the TextInputLayout and configuring the MaskType property in the Masked Entry, you can effectively limit the input length.

Step 1: XAML Setup
Begin by initializing a TextInputLayout containing a Masked Entry. Set the CharMaxLength property of the TextInputLayout to specify the maximum allowed character count, which in this example is defined as 10. Inside the layout, configure the MaskType property of the Masked Entry to “RegEx” to enable regular expression-based masking.

XAML

<StackLayout VerticalOptions="Center">
      <textinputlayout:SfTextInputLayout x:Name="inputLayout" 
                                         Hint="Enter phone number" 
                                         CharMaxLength="10">
              <syncfusion:SfMaskedEntry  MaskType="RegEx" 
                                         Mask="{Binding MaskChar}"/>
            </textinputlayout:SfTextInputLayout>
</StackLayout>

Step 2: C#

Create a property named MaskChar to bind the mask and define its behavior. This property should be updated according to the CharMaxLength value set in the TextInputLayout. Utilize the OnPropertyChanged method to trigger the binding update.

C#

public partial class MainPage : ContentPage
{
        private string maskChar;
        public string MaskChar
        {
            get { return maskChar; }
            set { maskChar = value; OnPropertyChanged("MaskChar");}
        }
 
        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;
            MaskChar = @"\d{0,"+ this.inputLayout.CharMaxLength + "}";
        }
 }

Output

ezgif.com-gif-maker.gif

Conclusion
I hope you enjoyed learning about how to limit the number of inputs in the .NET MAUI Masked Entry.

You can refer to our .NET MAUI Masked Entry feature tour page to learn about its other groundbreaking features. Explore our .NET MAUI Masked Entry documentation to understand how to present and manipulate data.

For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our .NET MAUI Masked Entry and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied