RegularExpressionValidator.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // System.Web.UI.WebControls.RegularExpressionValidator.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Text.RegularExpressions;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [ToolboxData("<{0}:RegularExpressionValidator runat=\"server\" "
  39. + "ErrorMessage=\"RegularExpressionValidator\">"
  40. + "</{0}:RegularExpressionValidator>")]
  41. public class RegularExpressionValidator : BaseValidator
  42. {
  43. public RegularExpressionValidator(): base()
  44. {
  45. }
  46. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  47. [Editor ("System.Web.UI.Design.WebControls.RegexTypeEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  48. [WebSysDescription ("A regular expression that is used to validate.")]
  49. public string ValidationExpression
  50. {
  51. get
  52. {
  53. object o = ViewState["ValidationExpression"];
  54. if(o != null)
  55. {
  56. return (string)o;
  57. }
  58. return String.Empty;
  59. }
  60. set
  61. {
  62. try
  63. {
  64. Regex.IsMatch("", value);
  65. } catch(Exception)
  66. {
  67. throw new HttpException(HttpRuntime.FormatResourceString("Validator_bad_regex", value));
  68. }
  69. ViewState["ValidationExpression"] = value;
  70. }
  71. }
  72. protected override void AddAttributesToRender(HtmlTextWriter writer)
  73. {
  74. base.AddAttributesToRender(writer);
  75. if(base.RenderUplevel)
  76. {
  77. writer.AddAttribute("evaluationfunction", "RegularExpressionValidatorEvaluateIsValid");
  78. string exp = ValidationExpression;
  79. if(exp.Length > 0)
  80. {
  81. writer.AddAttribute("validationexpression", exp);
  82. }
  83. }
  84. }
  85. protected override bool EvaluateIsValid ()
  86. {
  87. string ctrl = GetControlValidationValue (ControlToValidate);
  88. if (ctrl == null || ctrl.Trim ().Length == 0)
  89. return true;
  90. bool retVal;
  91. try {
  92. retVal = Regex.IsMatch (ctrl, "^" + ValidationExpression + "$");
  93. } catch (Exception) {
  94. retVal = true;
  95. }
  96. return retVal;
  97. }
  98. }
  99. }