locked
How to only restrict the users to enter only numbers RRS feed

  • Question

  • I have made an application in which users have to enter only numbers. I have set the inputscope to numbers but what about a laptop user he can enter chracters. What should I do to only restrict the user to enter numbers.

    In desktop application I did this

    char ch = e.KeyChar;

                if (!Char.IsDigit(ch) && ch != 8 && ch != 13)
                {
                    e.Handled = true;
                }


    pratik

    Friday, September 13, 2013 4:36 PM

Answers

  • Hi pratik ,

    I gave you the link.

    You can download it in your VS project using nuget.

    Have a look.,

    and you can use it in your xaml like the below.

     <callisto:NumericUpDown x:Name="Size" Text="{Binding Size,Mode=TwoWay}"   Maximum="99999999.999"    FontSize="18" TextAlignment="Right" Increment="10" Height="30" Width="192"  AcceptsReturn="False"  Background="White"   DecimalPlaces="3"   />

    Of course there are various of formats , I'm talking about the number.

    In the link provided there are other examples you can use.

    Hope this helped you

    thank you

    Sunday, September 15, 2013 5:24 AM
  • I think I got a solution to this problem what we do is change the input scope to number and if we want to remove specific characters from the text box we can use the text box text changed event and do the following code

    if (textBox1.Text.Contains("-"))
                    {

                        int index;
                        index = textBox1.Text.IndexOf("-");
                        textBox1.Text = textBox1.Text.Remove(index, 1);
                    }

    Similarly we can do it for any character we want.


    Pratik Mohapatra

    • Marked as answer by Pratik99 Friday, September 20, 2013 4:48 PM
    • Edited by Pratik99 Friday, September 20, 2013 4:49 PM
    Friday, September 20, 2013 4:48 PM

All replies

  • I think the following thread should help you: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/0d1d55f3-0006-4edb-b689-952f5caf2c89/only-number-input-in-textbox

    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    Friday, September 13, 2013 11:57 PM
    Moderator
  • Or perhaps you can use something which is ready and free. The numeric control from callisto framework

    Take a look at here

    https://github.com/timheuer/callisto

    Saturday, September 14, 2013 5:55 AM
  • Hey,

    Can you give me the link can you just tell me how I could do it. That will help me a lot


    pratik

    Saturday, September 14, 2013 7:03 AM
  • Hi pratik ,

    I gave you the link.

    You can download it in your VS project using nuget.

    Have a look.,

    and you can use it in your xaml like the below.

     <callisto:NumericUpDown x:Name="Size" Text="{Binding Size,Mode=TwoWay}"   Maximum="99999999.999"    FontSize="18" TextAlignment="Right" Increment="10" Height="30" Width="192"  AcceptsReturn="False"  Background="White"   DecimalPlaces="3"   />

    Of course there are various of formats , I'm talking about the number.

    In the link provided there are other examples you can use.

    Hope this helped you

    thank you

    Sunday, September 15, 2013 5:24 AM
  • The namespace prefix Callisto is not defined what should I do?

    pratik

    Friday, September 20, 2013 7:59 AM
  • hi ,

    Go to your XAML at the top of the page and put this

     xmlns:callisto="using:Callisto.Controls"

    and in your cs

    using Callisto.Controls;

    thank you

    Friday, September 20, 2013 8:15 AM
  • This makes its own textbox how do we inculcate in an existing textbox?

    pratik

    Friday, September 20, 2013 8:32 AM
  • You can use the callisto for the numbers and the normal textbox for the other stuff you want to do.

    FYI the callisto numeric control IS inherited from the textbox

    And if you go to Tim's site I think you have the source code so you can do whatever you like.

    thank you

    • Edited by zakkar Friday, September 20, 2013 11:06 AM
    Friday, September 20, 2013 11:04 AM
  • Nope that was not my question I want to know how can I enter only numbers in a textbox that was my original question

    pratik

    Friday, September 20, 2013 12:03 PM
  • I think I got a solution to this problem what we do is change the input scope to number and if we want to remove specific characters from the text box we can use the text box text changed event and do the following code

    if (textBox1.Text.Contains("-"))
                    {

                        int index;
                        index = textBox1.Text.IndexOf("-");
                        textBox1.Text = textBox1.Text.Remove(index, 1);
                    }

    Similarly we can do it for any character we want.


    Pratik Mohapatra

    • Marked as answer by Pratik99 Friday, September 20, 2013 4:48 PM
    • Edited by Pratik99 Friday, September 20, 2013 4:49 PM
    Friday, September 20, 2013 4:48 PM
  • Hi Pratik,

    Note that the InputScope is advisory only: it will affect the soft keyboard presentation, but it won't prevent the user from entering other characters from a physical keyboard, cut and paste, etc.

    You can block non-numeric characters in TextChanged so long as your requirements are simple. If they get more complicated (e.g. numbers in a specific range) so that an entry may change from invalid to valid (e.g. "1" may be out of range, but necessary to get to "100") then TextChanged won't work so well. For that case you can validate either when the TextBox loses focus or when the user accepts the page entry. The best usability is generally to validate and display an error when the contents are invalid but not to block the user from entering temporarily invalid values. Just don't let the user accept them and submit the form until everything is valid.

    --Rob

    Friday, September 20, 2013 7:53 PM
    Moderator