Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBlock accepting only digits in Windows Phone 7

I am creating an app in which I don't want alphabets or special characters to be used by the user except numbers(digits). The user can only enter digits ... if the user entered alphabets or special characters, then it will show an error message ... I have found the solution for the "null part" i.e.

if (uservalue == "" )
  textblock.text = "Sorry! enter digit please"
else
  textblock.text=y.toString();

If the user just press the "click me" button without entering the digit in the textbox, then this message "Sorry! enter digit please" appears in the textblock. I am wondering how can I fix the problem for alphabets and special characters?

like image 507
user1351579 Avatar asked Dec 07 '22 15:12

user1351579


1 Answers

You can change the keyboard of the phone, to display only digits, by adding the following in your TextBox:

<TextBox .... InputScope="Digits" ....>

This will still add the '.' key in the keyboard. To prevent users from typing it you add the KeyUp event to the TextBox and do the following:

private void KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    TextBox txt = (TextBox)sender;
    if (txt.Text.Contains('.'))
    {
        txt.Text = txt.Text.Replace(".", "");
        txt.SelectionStart = txt.Text.Length;
    }
}
like image 184
Dante Avatar answered Dec 09 '22 04:12

Dante