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?
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With