RangeValidator.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System;
  27. using System.ComponentModel;
  28. using System.Globalization;
  29. // Modeled after Nikhil Kothari's sample in "ASP Server Controls and Components", pp368
  30. namespace System.Web.UI.WebControls {
  31. [ToolboxData("<{0}:RangeValidator runat=server ErrorMessage=\"RangeValidator\"></{0}:RangeValidator>")]
  32. public class RangeValidator : BaseCompareValidator {
  33. #region Public Constructors
  34. public RangeValidator() {
  35. }
  36. #endregion // Public Constructors
  37. #region Public Instance Properties
  38. [Bindable(true)]
  39. [DefaultValue("")]
  40. [WebSysDescription ("")]
  41. [WebCategory ("Behavior")]
  42. public string MaximumValue {
  43. get {
  44. return ViewState.GetString("MaximumValue", String.Empty);
  45. }
  46. set {
  47. ViewState["MaximumValue"] = value;
  48. }
  49. }
  50. [Bindable(true)]
  51. [DefaultValue("")]
  52. [WebSysDescription ("")]
  53. [WebCategory ("Behavior")]
  54. public string MinimumValue {
  55. get {
  56. return ViewState.GetString("MinimumValue", String.Empty);
  57. }
  58. set {
  59. ViewState["MinimumValue"] = value;
  60. }
  61. }
  62. #endregion // Public Instance Properties
  63. #region Public Instance Methods
  64. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  65. base.AddAttributesToRender (writer);
  66. if (RenderUplevel) {
  67. writer.AddAttribute("evaluationfunction", "RangeValidatorEvaluateIsValid"); // FIXME - we need to define this in client code
  68. writer.AddAttribute("minimumValue", MinimumValue.ToString(CultureInfo.InvariantCulture));
  69. writer.AddAttribute("maximumValue", MaximumValue.ToString(CultureInfo.InvariantCulture));
  70. }
  71. }
  72. protected override bool ControlPropertiesValid() {
  73. if (!CanConvert(MinimumValue, this.Type)) {
  74. throw new HttpException("Minimum value cannot be converterd to type " + this.Type.ToString());
  75. }
  76. if (!CanConvert(MaximumValue, this.Type)) {
  77. throw new HttpException("Maximum value cannot be converterd to type " + this.Type.ToString());
  78. }
  79. if (this.Type != ValidationDataType.String) {
  80. if (Compare(MinimumValue, MaximumValue, ValidationCompareOperator.GreaterThan, this.Type)) {
  81. throw new HttpException("Maximum value must be equal or bigger than Minimum value");
  82. }
  83. }
  84. return base.ControlPropertiesValid ();
  85. }
  86. protected override bool EvaluateIsValid() {
  87. string control_value;
  88. control_value = GetControlValidationValue(this.ControlToValidate);
  89. if (control_value == null) {
  90. return true;
  91. }
  92. control_value = control_value.Trim();
  93. if (Compare(control_value, MinimumValue, ValidationCompareOperator.GreaterThanEqual, this.Type)) {
  94. if (Compare(control_value, MaximumValue, ValidationCompareOperator.LessThanEqual, this.Type)) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. #endregion // Public Instance Methods
  101. }
  102. }