Numeric input validator behavior


/ Published in: C#
Save to your folder(s)

behavior for validating numeric input


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Interactivity;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11.  
  12. namespace TestTextBox
  13. {
  14. public class NumericTextBoxValidator : Behavior<TextBox>
  15. {
  16.  
  17. public Type TargetType { get; set; }
  18.  
  19. protected override void OnAttached()
  20. {
  21. base.OnAttached();
  22.  
  23. AssociatedObject.Unloaded += ue_Unloaded;
  24. AssociatedObject.PreviewKeyDown += ue_PreviewKeyDown;
  25. AssociatedObject.PreviewTextInput += ue_PreviewTextInput;
  26. }
  27.  
  28. protected override void OnDetaching()
  29. {
  30. AssociatedObject.Unloaded -= ue_Unloaded;
  31. AssociatedObject.PreviewKeyDown -= ue_PreviewKeyDown;
  32. }
  33.  
  34. private void ue_PreviewTextInput(object sender, TextCompositionEventArgs e)
  35. {
  36. if (TargetType != null)
  37. {
  38. try
  39. {
  40. var resultingText = this.AssociatedObject.Text.Insert(this.AssociatedObject.CaretIndex, e.Text);
  41. Convert.ChangeType(resultingText, TargetType);
  42. }
  43. catch (Exception)
  44. {
  45. e.Handled = true;
  46. }
  47. }
  48. }
  49.  
  50. private void ue_Unloaded(object sender, RoutedEventArgs e)
  51. {
  52. var ue = sender as FrameworkElement;
  53. if (ue == null) return;
  54.  
  55. ue.Unloaded -= ue_Unloaded;
  56. ue.PreviewKeyDown -= ue_PreviewKeyDown;
  57. }
  58.  
  59. private void ue_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  60. {
  61. var ue = AssociatedObject;
  62. if (e.Key == Key.Space)
  63. {
  64. e.Handled = true;
  65. }
  66.  
  67. if (!IsKeyADigit(e.Key) && !IsKeyAllowedChar(e.Key) && e.Key != Key.Delete && e.Key != Key.Back && !IsNavigationKey(e.Key) && !IsControlKey(e.Key))
  68. {
  69. e.Handled = true;
  70. }
  71.  
  72. if (e.Key == Key.Back || e.Key == Key.Delete)
  73. {
  74. if (ue.SelectedText.Length == ue.Text.Length)
  75. {
  76. ue.Text = null;
  77. e.Handled = true;
  78. return;
  79. }
  80.  
  81. if (ue.Text.Length == 1)
  82. {
  83. ue.Text = null;
  84. e.Handled = true;
  85. }
  86. }
  87. else if ((IsKeyADigit(e.Key) || IsKeyAllowedChar(e.Key)) && ue.SelectedText.Length == ue.Text.Length)
  88. {
  89. ue.Text = null;
  90. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  91. {
  92. ue.Text = "-";
  93. ue.SelectionStart = 1;
  94. e.Handled = true;
  95. }
  96. else if (e.Key == Key.OemPeriod || e.Key == Key.OemComma || e.Key == Key.Decimal)
  97. {
  98. ue.Text = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
  99. ue.SelectionStart = 1;
  100. e.Handled = true;
  101. }
  102.  
  103. }
  104. }
  105.  
  106. private bool IsControlKey(Key key)
  107. {
  108. return key == Key.LeftCtrl || key == Key.RightCtrl || key == Key.RightAlt || key == Key.LeftAlt ||
  109. key == Key.RightShift || key == Key.LeftShift;
  110. }
  111.  
  112. private bool IsNavigationKey(Key key)
  113. {
  114. return key == Key.Left || key == Key.Right || key == Key.Return || key == Key.Down || key == Key.Up;
  115. }
  116.  
  117. public static bool IsKeyADigit(Key key)
  118. {
  119. return (key >= Key.D0 && key <= Key.D9) || (key >= Key.NumPad0 && key <= Key.NumPad9);
  120. }
  121.  
  122. public static bool IsKeyAllowedChar(Key key)
  123. {
  124. return (key == Key.OemMinus || key == Key.Subtract || key == Key.OemPeriod || key == Key.OemComma || key == Key.Decimal);
  125. }
  126. }
  127. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.