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.

331 wiersze
11KB

  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. '-----------------------------------------------------------------------------------------------------------------------
  209. ' Dictionary-based answer validation
  210. ' For validating a Scripting.Dictionary of loosely-typed values (e.g. a parsed JSON POST body)
  211. ' against field rules, accumulating messages into a ByRef errors() array - a different shape
  212. ' of validation than the instance/property Validator_Class above, which needs a live object
  213. ' instance and an Eval'able property name.
  214. '-----------------------------------------------------------------------------------------------------------------------
  215. Function IsValidEmail(email)
  216. Dim re
  217. Set re = New RegExp
  218. re.Pattern = "^[^\s@]+@[^\s@]+\.[^\s@]+$"
  219. re.IgnoreCase = True
  220. IsValidEmail = re.Test(email)
  221. End Function
  222. Sub AddValidationError(ByRef errors, msg)
  223. ReDim Preserve errors(UBound(errors) + 1)
  224. errors(UBound(errors)) = msg
  225. End Sub
  226. ' Returns the value for key in a Scripting.Dictionary, or Null if not present (e.g. a
  227. ' SurveyJS question skipped via visibleIf logic and so never included in the result data).
  228. Function DictValue(dict, key)
  229. If dict.Exists(key) Then
  230. DictValue = dict.Item(key)
  231. Else
  232. DictValue = Null
  233. End If
  234. End Function
  235. ' Required - always checked.
  236. Sub RequireNonEmptyString(ByRef errors, dict, key, msg)
  237. Dim v : v = DictValue(dict, key)
  238. If IsNull(v) Or Len(Trim(v & "")) = 0 Then AddValidationError errors, msg
  239. End Sub
  240. ' Only checked when present - callers may have fields that are conditionally hidden
  241. ' client-side, so a missing value just means the field wasn't shown, not an invalid submission.
  242. Sub RequireEnum(ByRef errors, dict, key, allowedValues)
  243. Dim v : v = DictValue(dict, key)
  244. If Not IsNull(v) Then
  245. Dim found : found = False
  246. Dim i
  247. For i = 0 To UBound(allowedValues)
  248. If CStr(v) = allowedValues(i) Then found = True
  249. Next
  250. If Not found Then AddValidationError errors, key & " has an invalid value."
  251. End If
  252. End Sub
  253. Sub RequireBoolean(ByRef errors, dict, key)
  254. Dim v : v = DictValue(dict, key)
  255. If Not IsNull(v) Then
  256. If TypeName(v) <> "Boolean" Then AddValidationError errors, key & " must be true or false."
  257. End If
  258. End Sub
  259. Sub RequireNonNegativeInteger(ByRef errors, dict, key)
  260. Dim v : v = DictValue(dict, key)
  261. If Not IsNull(v) Then
  262. If Not IsNumeric(v) Then
  263. AddValidationError errors, key & " must be a number."
  264. ElseIf CDbl(v) < 0 Then
  265. AddValidationError errors, key & " cannot be negative."
  266. ElseIf CDbl(v) <> Int(CDbl(v)) Then
  267. AddValidationError errors, key & " must be a whole number."
  268. End If
  269. End If
  270. End Sub
  271. Sub RequireValidDate(ByRef errors, dict, key)
  272. Dim v : v = DictValue(dict, key)
  273. If Not IsNull(v) Then
  274. If Len(Trim(v & "")) > 0 And Not IsDate(v) Then
  275. AddValidationError errors, key & " is not a valid date."
  276. End If
  277. End If
  278. End Sub
  279. %>

Powered by TurnKey Linux.