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:
https://github.com/MelbourneDeveloper/Adapt.Presentation/blob/master/Adapt.Presentation.Standard/Adapt/Presentation/Behaviours/EntryPatternMatchValidationBehaviour.cs

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">
                                <Entry.Behaviors>
                                    <behaviours:EntryPatternMatchValidationBehaviour Mask="^\d+(?:\.\d{0,2})?$" Default="0" />
                                </Entry.Behaviors>
                            </Entry>

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.
https://github.com/MelbourneDeveloper/Adapt.Presentation.git

Posts

Sign In or Register to comment.