MAUI Entry Control Keyboard to accept A TO Z Letters Only

70 Views Asked by At

I have a Entry control on MAUI Page to Accept User Name, for this I want the control to Accept A to Z Letters only (no numeric, symbol or special character allowed) and that too in Capital Letters

I wrote xaml like this

        <Entry x:Name="txtUserName" Placeholder="User Name" Text="{Binding UserName}" MaxLength="15">
            <Entry.Keyboard>
                <Keyboard x:FactoryMethod="Create">
                    <x:Arguments>
                        <KeyboardFlags>CapitalizeCharacter</KeyboardFlags>
                    </x:Arguments>
                </Keyboard>
            </Entry.Keyboard>
        </Entry>

this only Capitalizes letters, but numbers and symbols also get accepted, no way to restrict them found.

1

There are 1 best solutions below

0
Valerio On BEST ANSWER

I solved the same issue filtering the Text on the TextChanged Function :

    private void Letter_TextChanged(object sender, TextChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.NewTextValue))
    {
        var textFiltered= new string(e.NewTextValue.Where(char.IsLetter).ToArray());
        ((Entry)sender).Text = textFiltered;
    }
}

To implement it will be enough to use insert it in your XAML code the following way

        <Entry x:Name="txtUserName" Placeholder="User Name" Text="{Binding UserName}" MaxLength="15" TextChanged="Letter_TextChanged">
        <Entry.Keyboard>
            <Keyboard x:FactoryMethod="Create">
                <x:Arguments>
                    <KeyboardFlags>CapitalizeCharacter</KeyboardFlags>
                </x:Arguments>
            </Keyboard>
        </Entry.Keyboard>
    </Entry>