locked
Xamarin Forms Entry - Keyboard property not working in UWP RRS feed

  • Question

  • User369455 posted

    In Xaml Page,

    In Android, it shows the numeric keyboard but in Windows UWP app, entry is taking alphabets as well.

    Friday, June 8, 2018 1:13 PM

Answers

  • User53115 posted

    The Keyboard property doesn't filter out what keys the entry accepts, just which soft keyboard is presented. On an iPad you will have the same problem that the user can switch from the numeric/symbolic keyboard to the QWERTY keyboard and start inputting letters.

    The solution is to add a Behavior or Effect to the Entry to limit the input to only digits.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 8, 2018 1:56 PM
  • User89714 posted

    @abhishekaggarwal said: There's one thing I'm concerned about. I have already put TextChanged event handler for some Entries for which I want numeric input as well. Will both TextChanged work fine? If not, I guess I have to put the above code before my existing TextChanged event handler and put return after 'currentEntry.Text = previousString;'

    Each Entry can have its own TextChanged handler, or can share a TextChanged handler, or even have multiple TextChanged handlers (although I've never tried that), as appropriate.

    Alternatively, you might want to consider using Behaviors or Triggers instead. It all depends on your requirements and how sophisticated and enterprise-like you want to get. For details checkout the following links:

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/ https://blog.xamarin.com/validation-xamarin-forms-enterprise-apps/

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 12, 2018 1:25 PM

All replies

  • User53115 posted

    The Keyboard property doesn't filter out what keys the entry accepts, just which soft keyboard is presented. On an iPad you will have the same problem that the user can switch from the numeric/symbolic keyboard to the QWERTY keyboard and start inputting letters.

    The solution is to add a Behavior or Effect to the Entry to limit the input to only digits.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 8, 2018 1:56 PM
  • User89714 posted

    @abhishekaggarwal - For UWP, details of what keys are present on most of the different keyboards can be found at: https://msdn.microsoft.com/library/6e5f55d7-24d6-47cc-b457-b6231ede2a71 (caveat: it seems to be a bit dated, so expect some differences). Out of the box, not all UWP keyboard options are implemented using the Xamarin.Forms.Entry Keyboard property. If you want to use one of the UWP keyboards not implemented by Xamarin.Forms, you can subclass Entry and add a custom renderer that uses different values for InputScopeNameValue - see https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Input.InputScopeNameValue).

    However, depending on what your precise requirements are, you may find that none of the UWP keyboards are suitable. If you just want the digits 0-9 you can use the Number (or NumericPin etc) option. However, if you want decimal separators and thousands separators, then you will likely end up with a keyboard that can also enter alphabetic characters etc. The alternative, of course, is to build a custom keyboard, but if you do that do remember to support users in different countries, who may use different characters (for separators etc).

    Friday, June 8, 2018 3:23 PM
  • User369455 posted

    Can I just put regex validation in entry text changed event handler ??

    Monday, June 11, 2018 6:57 AM
  • User89714 posted

    @abhishekaggarwal said: Can I just put regex validation in entry text changed event handler ??

    Yes you can. The user might see typed characters appear and then disappear, but if you can accept then yes.

    Monday, June 11, 2018 8:22 AM
  • User369455 posted

    @JohnHardman said:

    @abhishekaggarwal said: Can I just put regex validation in entry text changed event handler ??

    Yes you can. The user might see typed characters appear and then disappear, but if you can accept then yes.

    public void Contact_TextChanged(object sender, EventArgs e)
        {
            var currentEntry = (Entry)sender;
    
            num = contact.Text;
            contact_regex = "^[0-9]*$";
    
            if (!Regex.IsMatch(num, contact_regex))
            {
                num = num.Substring(0, num.Length - 1);
                currentEntry.Text = num;
            }
        }
    

    @JohnHardman How about this? The sender itself is the entry, so it can be used for more than one Entry.

    Monday, June 11, 2018 9:54 AM
  • User89714 posted

    @abhishekaggarwal - That's assuming that the character just typed is always at the end of the string. That may not be the case.

    Monday, June 11, 2018 11:03 AM
  • User369455 posted

    @JohnHardman - The input will be given to the Entry char by char. Also, when the currentEntry.Text is set to num, TextChanged will be called ensuring that the new trimmed string is satisfying the regex.

    Monday, June 11, 2018 1:28 PM
  • User53115 posted

    Unless the user copy-pastes text into the Entry.

    Monday, June 11, 2018 1:34 PM
  • User369455 posted

    Suppose, you copy '12ab3x' On TextChanged, it will remove last char, and change the text to '12ab3' (while will fire another TextChanged).

    As '12ab3' will not satisfy regex, it will again remove last char. -> '12ab' Similar for '12ab', and '12a'

    As '12' will satisfy regex, the condition won't be true. Hence, '12' will appear, if the user copy-pastes text '12ab3x'

    I just have to put null or empty check matching regex.

    Monday, June 11, 2018 1:58 PM
  • User89714 posted

    @abhishekaggarwal - There's nothing stopping the user from moving the insertion point, using the cursor keys, the mouse, or by touch. That means that they can type the next character at any point in the text. For example, if I wanted to type "drain" but mistyped "dain", I could either backspace three times and then type "rain", or I could move the insertion point and just type the missing "r". That "r" wouldn't be the last character in the text.

    Monday, June 11, 2018 3:38 PM
  • User369455 posted

    @JohnHardman - It might give problem for other input types, but I don't see any problem if I want to accept only numerics

    Tuesday, June 12, 2018 4:57 AM
  • User89714 posted

    @abhishekaggarwal said: @JohnHardman - It might give problem for other input types, but I don't see any problem if I want to accept only numerics

    Same problem - the user wants to type 65536 but mistypes 6536. Many would just move the insertion point and type the missing 5.

    Tuesday, June 12, 2018 8:50 AM
  • User369455 posted

    @JohnHardman said:

    @abhishekaggarwal said: @JohnHardman - It might give problem for other input types, but I don't see any problem if I want to accept only numerics

    Same problem - the user wants to type 65536 but mistypes 6536. Many would just move the insertion point and type the missing 5.

    @JohnHardman - changing text from '6536' to '65536', the new string is also numeric, so it will satisfy regex.

    One minor problem could be if the user types 'a' instead of '5'. Then, the string will be trimmed to just '6'

    Tuesday, June 12, 2018 10:31 AM
  • User89714 posted

    @abhishekaggarwal said: One minor problem could be if the user types 'a' instead of '5'. Then, the string will be trimmed to just '6'

    That's why I said earlier in the thread about the code you posted "That's assuming that the character just typed is always at the end of the string. That may not be the case". You need to change your code to handle the general case rather than assuming new characters always appear at the end.

    Tuesday, June 12, 2018 10:36 AM
  • User369455 posted

    @JohnHardman said:

    @abhishekaggarwal said: One minor problem could be if the user types 'a' instead of '5'. Then, the string will be trimmed to just '6'

    That's why I said earlier in the thread about the code you posted "That's assuming that the character just typed is always at the end of the string. That may not be the case". You need to change your code to handle the general case rather than assuming new characters always appear at the end.

        string previousString = "";
    
        private void TaxEntry_TextChanged(object sender, TextChangedEventArgs e)
        {
            var currentEntry = (Entry)sender;
    
            string num = currentEntry.Text;
            string contact_regex = "^[0-9]*$";
    
            if (!string.IsNullOrEmpty(num))
            {
                if (!Regex.IsMatch(num, contact_regex))
                {                                          
                    currentEntry.Text = previousString;
                    return;
                }
            }
    
            previousString = num;
        }
    

    This solves normal case as well as that case in which the user will insert in between. Couple of things that I don't like about this are - 1. I have to use a variable outside the function, so I can't use it for multiple entries. 2. Suppose '6523'. Now, the user inserts 'a' after 5, it will remain '6523', but the cursor will move to the end of the input string.

    Tuesday, June 12, 2018 11:25 AM
  • User89714 posted

    @abhishekaggarwal said: 1. I have to use a variable outside the function, so I can't use it for multiple entries.

    Check the properties of TextChangedEventArgs

    @abhishekaggarwal said: 2. Suppose '6523'. Now, the user inserts 'a' after 5, it will remain '6523', but the cursor will move to the end of the input string.

    You can control the caret position in renderer code.

    Tuesday, June 12, 2018 11:33 AM
  • User369455 posted

    @JohnHardman said:

    @abhishekaggarwal said: 1. I have to use a variable outside the function, so I can't use it for multiple entries.

    Check the properties of TextChangedEventArgs

    @abhishekaggarwal said: 2. Suppose '6523'. Now, the user inserts 'a' after 5, it will remain '6523', but the cursor will move to the end of the input string.

    You can control the caret position in renderer code.

    private void TaxEntry_TextChanged(object sender, TextChangedEventArgs e)
        {
            string previousString = e.OldTextValue;
    
            var currentEntry = (Entry)sender;        
    
            string num = currentEntry.Text;
            string contact_regex = "^[0-9]*$";
    
            if (!string.IsNullOrEmpty(num))
            {
                if (!Regex.IsMatch(num, contact_regex))
                {                                          
                    currentEntry.Text = previousString;    
                }
            }
    
        }
    

    This resolves the first pointer. Is there any sophisticated way to do this, like the new text value will be consumed (not applied)?

    There's one thing I'm concerned about. I have already put TextChanged event handler for some Entries for which I want numeric input as well. Will both TextChanged work fine? If not, I guess I have to put the above code before my existing TextChanged event handler and put return after 'currentEntry.Text = previousString;'

    Tuesday, June 12, 2018 11:45 AM
  • User89714 posted

    @abhishekaggarwal said: There's one thing I'm concerned about. I have already put TextChanged event handler for some Entries for which I want numeric input as well. Will both TextChanged work fine? If not, I guess I have to put the above code before my existing TextChanged event handler and put return after 'currentEntry.Text = previousString;'

    Each Entry can have its own TextChanged handler, or can share a TextChanged handler, or even have multiple TextChanged handlers (although I've never tried that), as appropriate.

    Alternatively, you might want to consider using Behaviors or Triggers instead. It all depends on your requirements and how sophisticated and enterprise-like you want to get. For details checkout the following links:

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/ https://blog.xamarin.com/validation-xamarin-forms-enterprise-apps/

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 12, 2018 1:25 PM
  • User230728 posted

    So there isn't anything like PreviewTextInput in WPF?

    Friday, June 21, 2019 9:50 PM