Search code examples
xamarincross-platformxamarin.formsdata-entry

How to specify allowed characters in keyboard?


I want to create a page for pin entry for both, android and iOS platforms. Numeric specification in Keyboard property is close to my needs. I can do something like this to restrict allowed characters and overall length. However I need to get rid of the dot character on the keyboard. How can I achieve that?


Solution

  • You can remove the dot from the soft keyboard.

    Using the solution you linked and Keyboard="Numeric", you can use the same TextChanged event that restricts entry text size to restrict the '.'.

    Example:

    public void OnTextChanged(object sender, TextChangedEventArgs args)
    {
        var e = sender as Entry;
        string val = e.Text;
    
        if (string.IsNullOrEmpty(val))
        {
            return;
        }
    
        if (MaxLength > 0 && val.Length > MaxLength)
        {
            val = val.Remove(val.Length - 1);
        }
    
        if (val.Contains("."))
        {
            val.Replace(".", string.Empty);    
        }
        e.Text = val;
    }
    

    Other option would be creating a Grid for the PIN. And show the PIN in a Label instead of and Entry to avoid pasting.