Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

84 рядки
2.9KB

  1. <%
  2. '=======================================================================================================================
  3. ' FORMAT / PARSE HELPERS (generic, not tied to a table)
  4. '=======================================================================================================================
  5. ' VBScript has no IIf; this evaluates both branches, so only use it with plain values.
  6. Function Iif_(cond, whenTrue, whenFalse)
  7. If cond Then Iif_ = whenTrue Else Iif_ = whenFalse
  8. End Function
  9. ' True for a plain positive integer id (1-9 digits); use before trusting a route/form id.
  10. Function Valid_Id(s)
  11. Dim re : Set re = New RegExp
  12. re.Pattern = "^[0-9]{1,9}$"
  13. Valid_Id = re.Test(s & "")
  14. End Function
  15. ' Date -> "mm/dd/yyyy" for display ("" for null / invalid).
  16. Function Date_Fmt(d)
  17. If IsNull(d) Or IsEmpty(d) Then
  18. Date_Fmt = ""
  19. ElseIf IsDate(d) Then
  20. Date_Fmt = Right("0" & Month(d), 2) & "/" & Right("0" & Day(d), 2) & "/" & Year(d)
  21. Else
  22. Date_Fmt = ""
  23. End If
  24. End Function
  25. ' Date -> "yyyy-mm-dd" for HTML date inputs.
  26. Function Date_Iso(d)
  27. If IsNull(d) Or IsEmpty(d) Then
  28. Date_Iso = ""
  29. ElseIf IsDate(d) Then
  30. Date_Iso = Year(d) & "-" & Right("0" & Month(d), 2) & "-" & Right("0" & Day(d), 2)
  31. Else
  32. Date_Iso = ""
  33. End If
  34. End Function
  35. ' "yyyy-mm-dd" (HTML date input) -> Date, or Null when blank/invalid.
  36. Function Date_Parse(s)
  37. Dim re, m, y, mo, d
  38. Date_Parse = Null
  39. Set re = New RegExp
  40. re.Pattern = "^(\d{4})-(\d{2})-(\d{2})$"
  41. If Not re.Test(s & "") Then Exit Function
  42. Set m = re.Execute(s)(0)
  43. y = CInt(m.SubMatches(0)) : mo = CInt(m.SubMatches(1)) : d = CInt(m.SubMatches(2))
  44. If y < 2000 Or y > 2100 Or mo < 1 Or mo > 12 Or d < 1 Or d > 31 Then Exit Function
  45. If Month(DateSerial(y, mo, d)) <> mo Then Exit Function
  46. Date_Parse = DateSerial(y, mo, d)
  47. End Function
  48. ' HTML-encodes text and turns line breaks into <br>.
  49. Function Html_Nl2Br(s)
  50. Html_Nl2Br = Replace(Replace(Replace(Server.HTMLEncode(s & ""), vbCrLf, "<br>"), vbLf, "<br>"), vbCr, "<br>")
  51. End Function
  52. ' Quotes a value for CSV. Values that a spreadsheet would treat as a formula are prefixed with an apostrophe
  53. ' (CSV injection), except ordinary phone numbers like +1 (555) 123-4567.
  54. Function Csv_Cell(v)
  55. Dim s, first, re
  56. If IsNull(v) Or IsEmpty(v) Then
  57. Csv_Cell = ""
  58. Exit Function
  59. End If
  60. s = CStr(v)
  61. If Len(s) > 0 Then
  62. first = Left(s, 1)
  63. If first = "=" Or first = "@" Or first = vbTab Or first = vbCr Then
  64. s = "'" & s
  65. ElseIf first = "+" Or first = "-" Then
  66. Set re = New RegExp
  67. re.Pattern = "^[+\-][0-9 ().\-]*$"
  68. If Not re.Test(s) Then s = "'" & s
  69. End If
  70. End If
  71. If InStr(s, ",") > 0 Or InStr(s, """") > 0 Or InStr(s, vbCr) > 0 Or InStr(s, vbLf) > 0 Or Left(s, 1) = " " Or Right(s, 1) = " " Then
  72. s = """" & Replace(s, """", """""") & """"
  73. End If
  74. Csv_Cell = s
  75. End Function
  76. %>

Powered by TurnKey Linux.