How can I restrict the length and characters entered in an Entry control in Xamarin.Forms. Do I need to create a custom control? Is there a way I can derive from Entry (or another control) so I can apply the necessary per-platform input limitations.
An example would be a numeric field that is restricted to a maximum of 3 characters, digits only.
Setting the Keyboard property of an Entry control to Keyboard.Numeric only sets the keyboard for iOS. It does not restrict the actual text entry - i.e. I can still enter non-digit characters. Nor do I see a way to limit the length of entry.
You can restrict the number of charecters in the Entry field as given below,
int restrictCount = <your restriction length> //Enter your number of character restriction Entry entry = new Entry(); entry.TextChanged += OnTextChanged; void OnTextChanged(object sender, EventArgs e) { Entry entry = sender as Entry; String val = entry.Text; //Get Current Text if(val.Length > restrictCount)//If it is more than your character restriction { val = val.Remove(val.Length - 1);// Remove Last character entry.Text = val; //Set the Old value } }
I would use Behaviors. More about it: https://developer.xamarin.com/guides/xamarin-forms/behaviors/creating/
Example is for Entry with Numeric keyboard. But it can be used for any keyboard.
XAML usage:
<ContentPage xmlns:behaviors="clr-namespace:myApp.Behaviors;assembly=myApp" <Entry Keyboard="Numeric" Text="{Binding EntryText}" > <Entry.Behaviors> <behaviors:EntryLengthValidatorBehavior MaxLength="3" /> </Entry.Behaviors> </Entry>
Behavior
public class EntryLengthValidatorBehavior : Behavior<Entry> { public int MaxLength { get; set; } protected override void OnAttachedTo(Entry bindable) { base.OnAttachedTo(bindable); bindable.TextChanged += OnEntryTextChanged; } protected override void OnDetachingFrom(Entry bindable) { base.OnDetachingFrom(bindable); bindable.TextChanged -= OnEntryTextChanged; } void OnEntryTextChanged(object sender, TextChangedEventArgs e) { var entry = (Entry)sender; // if Entry text is longer then valid length if (entry.Text.Length > this.MaxLength) { string entryText = entry.Text; entryText = entryText.Remove(entryText.Length - 1); // remove last char entry.Text = entryText; } } }
You can just use Binding; For example i want to hold a payment value that can not exceed 100. So i wrote a class
puclic class Payment : INotifyPropertyChanged
{
private int _amountDecimals;
public int AmountDecimals
{
get
{
return _amountDecimals;
}
set
{
if (value <= 100)
{
_amountDecimals = value;
}
OnPropertyChanged();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
So this property will set AmountDecimals value if user enters a value until it not exceeds 100
Then just set binding via code on Page constructor(or from xaml)
var myPayment =new Payment(); //this will hold page(view) data
BindingContext = myPayment;
var paymentEntry = new Entry();
paymentEntry.Keyboard = Keyboard.Numeric;
paymentEntry.SetBinding(Entry.TextProperty, "AmountDecimals");
So user enters a numeric value to the entry, but if he/she tries to enter a value more than 100, binding just reverse it to old value. You can just write your code to your class's properties like this (on setters). So if you want to some property to carry only 5 characters you can write something like this (codes can be wrong i did not compiled them :) )
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if ((value!= null && value.length <= 5) || value == null)
{
_name = value;
}
OnPropertyChanged();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With