Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

lib.Validations.asp 7.9KB

8 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <%
  2. '=======================================================================================================================
  3. ' Validation Classes
  4. '=======================================================================================================================
  5. '-----------------------------------------------------------------------------------------------------------------------
  6. ' Exists Validation
  7. '-----------------------------------------------------------------------------------------------------------------------
  8. Class ExistsValidation_Class
  9. Private m_instance
  10. Private m_field_name
  11. Private m_message
  12. Private m_ok
  13. Public Function Initialize(instance, field_name, message)
  14. set m_instance = instance
  15. m_field_name = field_name
  16. m_message = message
  17. m_ok = true
  18. set Initialize = Me
  19. End Function
  20. Public Sub Check
  21. If Len(eval("m_instance." & m_field_name)) = 0 then
  22. m_ok = false
  23. End If
  24. End Sub
  25. Public Property Get OK
  26. OK = m_ok
  27. End Property
  28. Public Property Get Message
  29. Message = m_message
  30. End Property
  31. End Class
  32. Sub ValidateExists(instance, field_name, message)
  33. if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
  34. instance.Validator.AddValidation new ExistsValidation_Class.Initialize(instance, field_name, message)
  35. End Sub
  36. '-----------------------------------------------------------------------------------------------------------------------
  37. ' Minimum Length Validation
  38. '-----------------------------------------------------------------------------------------------------------------------
  39. Class MinLengthValidation_Class
  40. Private m_instance
  41. Private m_field_name
  42. Private m_size
  43. Private m_message
  44. Private m_ok
  45. Public Function Initialize(instance, field_name, size, message)
  46. set m_instance = instance
  47. m_field_name = field_name
  48. m_size = size
  49. m_message = message
  50. m_ok = true
  51. set Initialize = Me
  52. End Function
  53. Public Sub Check
  54. If Len(eval("m_instance." & m_field_name)) < m_size then m_ok = false
  55. End Sub
  56. Public Property Get OK
  57. OK = m_ok
  58. End Property
  59. Public Property Get Message
  60. Message = m_message
  61. End Property
  62. End Class
  63. Sub ValidateMinLength(instance, field_name, size, message)
  64. if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
  65. instance.Validator.AddValidation new MinLengthValidation_Class.Initialize(instance, field_name, size, message)
  66. End Sub
  67. '-----------------------------------------------------------------------------------------------------------------------
  68. ' Max Length Validation
  69. '-----------------------------------------------------------------------------------------------------------------------
  70. Class MaxLengthValidation_Class
  71. Private m_instance
  72. Private m_field_name
  73. Private m_size
  74. Private m_message
  75. Private m_ok
  76. Public Function Initialize(instance, field_name, size, message)
  77. set m_instance = instance
  78. m_field_name = field_name
  79. m_size = size
  80. m_message = message
  81. m_ok = true
  82. set Initialize = Me
  83. End Function
  84. Public Sub Check
  85. If Len(eval("m_instance." & m_field_name)) > m_size then m_ok = false
  86. End Sub
  87. Public Property Get OK
  88. OK = m_ok
  89. End Property
  90. Public Property Get Message
  91. Message = m_message
  92. End Property
  93. End Class
  94. Sub ValidateMaxLength(instance, field_name, size, message)
  95. if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
  96. instance.Validator.AddValidation new MaxLengthValidation_Class.Initialize(instance, field_name, size, message)
  97. End Sub
  98. '-----------------------------------------------------------------------------------------------------------------------
  99. ' Numeric Validation
  100. '-----------------------------------------------------------------------------------------------------------------------
  101. Class NumericValidation_Class
  102. Private m_instance
  103. Private m_field_name
  104. Private m_message
  105. Private m_ok
  106. Public Function Initialize(instance, field_name, message)
  107. set m_instance = instance
  108. m_field_name = field_name
  109. m_message = message
  110. m_ok = true
  111. set Initialize = Me
  112. End Function
  113. Public Sub Check
  114. If Not IsNumeric(eval("m_instance." & m_field_name)) then m_ok = false
  115. End Sub
  116. Public Property Get OK
  117. OK = m_ok
  118. End Property
  119. Public Property Get Message
  120. Message = m_message
  121. End Property
  122. End Class
  123. Sub ValidateNumeric(instance, field_name, message)
  124. if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
  125. instance.Validator.AddValidation new NumericValidation_Class.Initialize(instance, field_name, message)
  126. End Sub
  127. '-----------------------------------------------------------------------------------------------------------------------
  128. ' Regular Expression Pattern Validation
  129. '-----------------------------------------------------------------------------------------------------------------------
  130. Class PatternValidation_Class
  131. Private m_instance
  132. Private m_field_name
  133. Private m_pattern
  134. Private m_message
  135. Private m_ok
  136. Public Function Initialize(instance, field_name, pattern, message)
  137. set m_instance = instance
  138. m_field_name = field_name
  139. m_pattern = pattern
  140. m_message = message
  141. m_ok = true
  142. set Initialize = Me
  143. End Function
  144. Public Sub Check
  145. dim re : set re = new RegExp
  146. With re
  147. .Pattern = m_pattern
  148. .Global = true
  149. .IgnoreCase = true
  150. End With
  151. dim matches : set matches = re.Execute(eval("m_instance." & m_field_name))
  152. if matches.Count = 0 then
  153. m_ok = false
  154. end if
  155. End Sub
  156. Public Property Get OK
  157. OK = m_ok
  158. End Property
  159. Public Property Get Message
  160. Message = m_message
  161. End Property
  162. End Class
  163. Sub ValidatePattern(instance, field_name, pattern, message)
  164. if not IsObject(instance.Validator) then set instance.Validator = new Validator_Class
  165. instance.Validator.AddValidation new PatternValidation_Class.Initialize(instance, field_name, pattern, message)
  166. End Sub
  167. '-----------------------------------------------------------------------------------------------------------------------
  168. ' Validator Class
  169. ' This class is not intended to be used directly. Models should use the Validate* subs instead.
  170. '-----------------------------------------------------------------------------------------------------------------------
  171. Class Validator_Class
  172. Private m_validations
  173. Private m_errors
  174. Private Sub Class_Initialize
  175. m_validations = Array()
  176. redim m_validations(-1)
  177. m_errors = Array()
  178. redim m_errors(-1)
  179. End Sub
  180. Public Property Get Errors
  181. Errors = m_errors
  182. End Property
  183. Public Sub AddValidation(validation)
  184. dim n : n = ubound(m_validations) + 1
  185. redim preserve m_validations(n)
  186. set m_validations(n) = validation
  187. End Sub
  188. Public Sub Validate
  189. dim n : n = ubound(m_validations)
  190. dim i, V
  191. for i = 0 to n
  192. set V = m_validations(i)
  193. V.Check
  194. if not V.OK then
  195. AddError V.Message
  196. end if
  197. next
  198. End Sub
  199. Public Property Get HasErrors
  200. HasErrors = (ubound(m_errors) > -1)
  201. End Property
  202. 'Public to allow other errors to be added by the controller for circumstances not accounted for by the validators
  203. Public Sub AddError(msg)
  204. redim preserve m_errors(ubound(m_errors) + 1)
  205. m_errors(ubound(m_errors)) = msg
  206. End Sub
  207. End Class
  208. %>

Powered by TurnKey Linux.