code_junkie

Asp.Net : Extended range validation

Nov 12th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. void ValidateRange(object sender, ServerValidateEventArgs e)
  2. {
  3. int input;
  4. bool parseOk = int.TryParse(e.Value, out input);
  5. e.IsValid = parseOk &&
  6. ((input >= 100 || input <= 200) ||
  7. (input >= 500 || input <= 600));
  8. }
  9.  
  10. ^(1d{2}|200|5d{2}|600)$
  11.  
  12. public class RangeValidatorEx : BaseValidator
  13. {
  14.  
  15. protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
  16. {
  17. base.AddAttributesToRender(writer);
  18.  
  19. if (base.RenderUplevel)
  20. {
  21. string clientId = this.ClientID;
  22.  
  23. // The attribute evaluation funciton holds the name of client-side js function.
  24. Page.ClientScript.RegisterExpandoAttribute(clientId, "evaluationfunction", "RangeValidatorEx");
  25.  
  26. Page.ClientScript.RegisterExpandoAttribute(clientId, "Range1High", this.Range1High.ToString());
  27. Page.ClientScript.RegisterExpandoAttribute(clientId, "Range2High", this.Range2High.ToString());
  28. Page.ClientScript.RegisterExpandoAttribute(clientId, "Range1Low", this.Range1Low.ToString());
  29. Page.ClientScript.RegisterExpandoAttribute(clientId, "Range2Low", this.Range2Low.ToString());
  30.  
  31. }
  32. }
  33.  
  34. // Will be invoked to validate the parameters
  35. protected override bool ControlPropertiesValid()
  36. {
  37. if ((Range1High <= 0) || (this.Range1Low <= 0) || (this.Range2High <= 0) || (this.Range2Low <= 0))
  38. throw new HttpException("The range values cannot be less than zero");
  39.  
  40. return base.ControlPropertiesValid();
  41. }
  42.  
  43. // used to validation on server-side
  44. protected override bool EvaluateIsValid()
  45. {
  46. int code;
  47. if (!Int32.TryParse(base.GetControlValidationValue(ControlToValidate), out code))
  48. return false;
  49.  
  50. if ((code < this.Range1High && code > this.Range1Low) || (code < this.Range2High && code > this.Range2Low))
  51. return true;
  52. else
  53. return false;
  54. }
  55.  
  56. // inject the client-side script to page
  57. protected override void OnPreRender(EventArgs e)
  58. {
  59. base.OnPreRender(e);
  60.  
  61. if (base.RenderUplevel)
  62. {
  63. this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RangeValidatorEx", RangeValidatorExJs(),true);
  64. }
  65. }
  66.  
  67.  
  68. string RangeValidatorExJs()
  69. {
  70. string js;
  71. // the validator will be rendered as a SPAN tag on the client-side and it will passed to the validation function.
  72. js = "function RangeValidatorEx(val){ "
  73. + " var code=document.getElementById(val.controltovalidate).value; "
  74. + " if ((code < rangeValidatorCtrl.Range1High && code > rangeValidatorCtrl.Range1Low ) || (code < rangeValidatorCtrl.Range2High && code > rangeValidatorCtrl.Range2Low)) return true; else return false;}";
  75. return js;
  76. }
  77.  
  78.  
  79. public int Range1Low
  80. {
  81. get {
  82. object obj2 = this.ViewState["Range1Low"];
  83.  
  84. if (obj2 != null)
  85. return System.Convert.ToInt32(obj2);
  86.  
  87. return 0;
  88.  
  89. }
  90. set { this.ViewState["Range1Low"] = value; }
  91. }
  92.  
  93. public int Range1High
  94. {
  95. get
  96. {
  97. object obj2 = this.ViewState["Range1High"];
  98.  
  99. if (obj2 != null)
  100. return System.Convert.ToInt32(obj2);
  101.  
  102. return 0;
  103.  
  104. }
  105. set { this.ViewState["Range1High"] = value; }
  106. }
  107. public int Range2Low
  108. {
  109. get
  110. {
  111. object obj2 = this.ViewState["Range2Low"];
  112.  
  113. if (obj2 != null)
  114. return System.Convert.ToInt32(obj2);
  115.  
  116. return 0;
  117.  
  118. }
  119. set { this.ViewState["Range2Low"] = value; }
  120. }
  121. public int Range2High
  122. {
  123. get
  124. {
  125. object obj2 = this.ViewState["Range2High"];
  126.  
  127. if (obj2 != null)
  128. return System.Convert.ToInt32(obj2);
  129.  
  130. return 0;
  131.  
  132. }
  133. set { this.ViewState["Range2High"] = value; }
  134. }
  135. }
Add Comment
Please, Sign In to add comment