Forum Xamarin.Forms

Masked Entry Behaviour (Field Level Masking/Validation)

We've created a behavior here that will be useful for a lot of people:

The Entry control seems to lack a fair bit of basic functionality. One of those lacking features is the ability to specify a pattern matching Mask to stop the user from entering silly input. This behavior allows you to specify a Mask that will allow you to validate things like numbers at the field level. The Mask is specified in RegEx so it's very flexible.

Here is a XAML example that forces the user to only input a valid decimal number with two decimal places. I.e. non-numeric characters can not be entered:

                            <Entry Grid.Row="2" Text="{Binding Qty, Mode=TwoWay}" VerticalOptions="Center" Keyboard="Numeric">
                                    <behaviours:EntryPatternMatchValidationBehaviour Mask="^\d+(?:\.\d{0,2})?$" Default="0" />

To see the sample, clone this repo which is a Xamarin library with a bunch of stuff that doesn't come out of the box in Xamarin. Try the Validation tab for this sample.


Sign In or Register to comment.