Silverlight文本框输入验证

01  //验证输入为数字 

 

02  private  void txt_time_KeyDown( object sender, KeyEventArgs e) 

03 { 

04  if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))) 

05 { 

06 e.Handled =  true

07 } 

08 } 

09  

10  //屏蔽粘贴非法字符 

11  private  void txt_time_TextChanged( object sender, TextChangedEventArgs e) 

12 { 

13 var textBox = sender  as TextBox; 

14 TextChange[] change =  new TextChange[e.Changes.Count]; 

15 e.Changes.CopyTo(change, 0); 

16  

17  int offset = change[0].Offset; 

18  if (change[0].AddedLength > 0) 

19 { 

20  double num = 0; 

21  if (!Double.TryParse(textBox.Text,  out num)) 

22 { 

23 textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); 

24 textBox.Select(offset, 0); 

25 } 

26 } 

27 } 

 

 

01  //屏蔽非法按键 

02  private  void txtAge_KeyDown( object sender, KeyEventArgs e) 

03 { 

04 TextBox txt = sender  as TextBox; 

05  

06  if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal) 

07 { 

08  if (txt.Text.Contains( ".") && e.Key == Key.Decimal) 

09 { 

10 e.Handled =  true

11  return

12 } 

13 e.Handled =  false

14 } 

15  else  if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift) 

16 { 

17  if (txt.Text.Contains( ".") && e.Key == Key.OemPeriod) 

18 { 

19 e.Handled =  true

20  return

21 } 

22 e.Handled =  false

23 } 

24  else 

25 { 

26 e.Handled =  true

27 } 

28 } 

29  

30  //屏蔽中文输入和非法字符粘贴输入 

31  private  void txtAge_TextChanged( object sender, TextChangedEventArgs e) 

32 { 

33 TextBox textBox = sender  as TextBox; 

34 TextChange[] change =  new TextChange[e.Changes.Count]; 

35 e.Changes.CopyTo(change, 0); 

36  

37  int offset = change[0].Offset; 

38  if (change[0].AddedLength > 0) 

39 { 

40  double num = 0; 

41  if (!Double.TryParse(textBox.Text,  out num)) 

42 { 

43 textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); 

44 textBox.Select(offset, 0); 

45 } 

46 } 

47 } 
 
 

本文来自edwinsoft的博客,原文地址:http://blog.sina.com.cn/s/blog_6506a31f0100qgp1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值