Xamarin Blog Post Image Logo

Often times while building mobile apps in Xamarin.Forms you get a requirement to restrict certain characters in input, this is not provided in Xamarin.Forms out of the box to disable certain characters in Entry control but Xamarin provides Behaviors which we can apply on Entry controls and achieve the desired result.

First of all lets create a sample project and add an Entry to our page.

First of all lets create a sample page and add an Entry.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Sample.Mobile.Forms.Controls;assembly=Sample.Mobile.Forms"
xmlns:extensions="clr-namespace:Sample.Mobile.Forms.Extensions;assembly=Sample.Mobile.Forms"
xmlns:behaviors="clr-namespace:Sample.Mobile.Forms.Behaviors;assembly=Sample.Mobile.Forms"
x:Class="Sample.Mobile.Forms.Views.AuditPage">
<ContentPage.Content>
<StackLayout>
<Entry HorizontalOptions="Fill"
Keyboard="Text"
ReturnType="Search">
</Entry>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Now we need to add a behaviour in which will Extend Behavior<Entry>
and implement OnAttached/OnDetached and OnEntryTextChanged events
public class SpecialCharactersValidationBehavior : Behavior<Entry>
{
//Here we have added the characters we wanted to restrict while entering data
private const string SpecialCharacters = @"'/\%*‘;$£&#^@|?+=<>\""";
protected override void OnAttachedTo(Entry entry)
{
entry.TextChanged += OnEntryTextChanged;
base.OnAttachedTo(entry);
}
protected override void OnDetachingFrom(Entry entry)
{
entry.TextChanged -= OnEntryTextChanged;
base.OnDetachingFrom(entry);
}
In this behaviour you can see that we are just validating that
no restricted characters gets to entry input.
private static void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
if (!string.IsNullOrWhiteSpace(args.NewTextValue))
{
bool isValid = args.NewTextValue.ToCharArray().All(x => !SpecialCharacters.Contains(x));
((Entry)sender).Text = isValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length - 1);
}
}
}
After this we just need to apply this behavior to our entry control
in our Xaml, here is how to do it
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Sample.Mobile.Forms.Controls;assembly=Sample.Mobile.Forms"
xmlns:extensions="clr-namespace:Sample.Mobile.Forms.Extensions;assembly=Sample.Mobile.Forms"
xmlns:behaviors="clr-namespace:Sample.Mobile.Forms.Behaviors;assembly=Sample.Mobile.Forms"
x:Class="Sample.Mobile.Forms.Views.AuditPage">
<ContentPage.Content>
<StackLayout>
<Entry HorizontalOptions="Fill"
Keyboard="Text"
ReturnType="Search">
<Entry.Behaviors>
<behaviors:SpecialCharactersValidationBehavior />
</Entry.Behaviors>
</Entry>
</StackLayout>
</ContentPage.Content>
</ContentPage>

Behavior in action, you can see in demo below that entry control is not allowing us to enter restricted characters:

Behavior in action

Behaviors in Xamarin.Forms are very powerful feature of adding functionality to UI controls without have to subclass them. You can read more about behavior here.

Greenfinch Technology

Greenfinch Technology