integer datatype Textbox Validation

Posted by Rohithrs under Silverlight on 8/5/2011 | Points: 10 | Views : 2592 | Status : [Member] | Replies : 1
Hi,
i am new to Silverlight, i created Silverlight application(MVVM pattern) contains Textbox and button.
the textbox datatype is int, so user should not enter any character in textbox, for this i neetd to do validation.

Can any one help me out on this(in detail).




Responses

Posted by: Vuyiswamb on: 10/11/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
This should do the Trick


 private void Textbox1_KeyDown(object sender, KeyEventArgs e)

{
if (e.Key == Key.Tab)
{
return;
}
var thisKeyStr = "";
if (e.PlatformKeyCode == 190 || e.PlatformKeyCode == 110)
{
thisKeyStr = ".";
}
else
{
thisKeyStr = e.Key.ToString().Replace("D", "").Replace("NumPad", "");
}
var s = (sender as TextBox).Text + thisKeyStr;
var rStr = "^[0-9]+[.]?[0-9]*$";
var r = new Regex(rStr, RegexOptions.IgnoreCase);
e.Handled = !r.IsMatch(s);
}


Thank you for posting at Dotnetfunda
[Administrator]

Rohithrs, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response