常用的函数

<%

'本文来自www.sql163.com

'==================================================================================================
'将传入的参数转换成整数, 并限制允许的上下限
'@param: number, 需要转换的参数
Function ToInt(number, min, max)
 number = ToLng(number)

 If IsNumeric(min) Then
  min = ToLng(min)
  If number < min Then
   number = min
  End If
 End If

 If IsNumeric(max) Then
  max = ToLng(max)
  If number > max Then
   number = max
  End If
 End If

 ToInt = number
End Function

 

'==================================================================================================
'非法字符串过虑
'@param: str, 需要过虑的字符串
'@param: length, 字符串允许的最大长度
Function strip(str, length)
 dim beginLen

 If VarType(str) <> vbString Then
  strip = ""
  Exit Function
 End If

 If IsNumeric(length) Then
  length = ToLng(length)
 Else
  length = 0
 End If

 str = Trim(str)
 beginLen = Len(str)
 str = Replace(str, "'", "''")

 If length > 0 Then
  '发生了以 ('') 替换 (') 后, 允许的最大长度要加上因替换而增加的字符长度
  length = length + Len(str) - beginLen
  str = Left(str, length)
 End If

 strip = str
End Function


'字符串截取
'@param: str, 需要截取的字符串
'@param: length, 截取的长度
Function substr(str, length)
 If VarType(str) <> vbString Then
  substr = ""
  Exit Function
 End If

 If IsNumeric(length) Then
  length = ToLng(length)
 Else
  length = 0
 End If

 If length < len(str) Then
  str = Left(str, length) & ".."
 End If

 substr = str
End Function


'字符串截取,去除其中的html代码
'@param: str, 需要截取的字符串
'@param: length, 截取的长度
Function substr2(str, length)
 If VarType(str) <> vbString Then
  substr2 = ""
  Exit Function
 End If

 If IsNumeric(length) Then
  length = ToLng(length)
 Else
  length = 0
 End If


 dim reg
 set reg = New RegExp
 reg.IgnoreCase = true
 reg.Global = True

 reg.Pattern = "<script[^>]*?>.*?</script>"
 str = reg.Replace(str, "")
 
 reg.Pattern = "<[^<>]*>"
 str = reg.Replace(str, "")

 set reg = nothing

 str = replace(str, "&nbsp;", " ")

 If length < len(str) Then
  str = Left(str, length) & "..."
 End If

 str = replace(str, " ", "&nbsp;")

 substr2 = str
End Function

'将传入的参数转换成整数
'@param: number, 需要转换的参数
Function ToLng(number)
 Select Case VarType(number)
  case vbString: ToLng = StringToLng(number)  
  case vbByte: ToLng = number
  case vbInteger: ToLng = number
  case vbLong: ToLng = number
  case vbCurrency: ToLng = BigNumberToLng(number)
  case vbSingle: ToLng = BigNumberToLng(number)
  case vbDouble: ToLng = BigNumberToLng(number)
  case else: ToLng = 0
 End Select
End Function

'将传入的字符串参数转换成整数, 超出 vbLng 范围的返回 0
'@param: number, 需要转换的字符串
Function StringToLng(number)
 If not IsNumeric(number) Then
  StringToLng = 0
  Exit Function
 End If

 If left(number, 1) = "-" Then
  If Len(number) > 11 Then
   StringToLng = 0
   Exit Function
  End If

  If Clng(Left(number, 10)) < -214748364 Then
   StringToLng = 0
   Exit Function
  End If

  If Clng(Left(number, 10)) = -214748364 and Clng(Right(number, 1)) > 8 Then
   StringToLng = 0
   Exit Function
  End If
 Else
  If Len(number) > 10 Then
   StringToLng = 0
   Exit Function
  End If

  If Clng(Left(number, 9)) > 214748364 Then
   StringToLng = 0
   Exit Function
  End If

  If Clng(Left(number, 9)) > 214748364 and Clng(right(number, 1)) > 7 Then
   StringToLng = 0
   Exit Function
  End If
 End If

 StringToLng = Clng(number)
End Function


'将传入的浮点数转换成整数, 超出 vbLng 范围的返回 0
'@param: number, 需要转换的参数, 是 vbSingle、vbDoubtl、vbCurrency 其中的一种可能
Function BigNumberToLng(number)
 If number > 2147483647 Then
  BigNumberToLng = 0
  Exit Function
 End If

 If number < -2147483648 Then
  BigNumberToLng = 0
  Exit Function
 End If

 BigNumberToLng = Clng(number)
End Function


'将传入的参数转换成浮点数
'@param: number, 需要转换的参数
Function ToDbl(number)
 If not IsNumeric(number) Then
  ToDbl = 0
 Else
  ToDbl = CDbl(number)
 End If
End Function


'日期输出格式
'StrDt:日期
'IntNum:日期样式
Function ShowDateTime(StrDT,IntNum)
  select case IntNum
 case 0:ShowDateTime = year(StrDT)&"-"&month(StrDT)&"-"&day(StrDT) '2006-6-6
 case 1:ShowDateTime = month(StrDT)&"-"&day(StrDT)     '6-6
 case 2:ShowDateTime = "["&month(StrDT)&"-"&day(StrDT)&"]"   '[6-6]
 case 3:ShowDateTime = "("&month(StrDT)&"月"&day(StrDT)&"日)"   '[6月6日]
  end select
End Function


'操作提示信息
Function ShowInfo(info)
 Dim TxtInfo
 select case info
  case "err"
   TxtInfo = "<font color=red>添加信息失败,请确认数据后重新再试!</font>"
  case "addok"
   TxtInfo = "<font color=red>信息添加完成,请继续!</font>"
  case "upok"
   TxtInfo = "<font color=red>信息修改完成,请继续!</font>"
  case "exist"
   TxtInfo = "<font color=red>你添加的信息已存在,请重新输入!</font>"
  case else
   TxtInfo = "<font color=red>"&Info&"</font>"
 end select
 ShowInfo = TxtInfo
end function


'***************************************************
'函数名:ShowMsgBox
'作  用:显示提示信息
'参  数:MsgType  --------提示类别.Ok为正确,err为错误;
'  Message  --------消息的内容;
'  Url   --------转向新的地址;
'返回值:无
'***************************************************
Sub ShowMsgBox(MsgType,Message,Url)
  Select Case MsgType
 Case "Msg"
  Response.Write ("<script>window.location.href='"&Url&"';</script>")
  Response.End
 Case "ok"
  Response.Write ("<script>alert('"&Message&"');window.location.href='"&Url&"';</script>")
  Response.End
 Case "err"
  Response.Write ("<script>alert('"&Message&"');history.back();</script>")
  Response.End
 Case else
  Response.Write ("<script>alert('你的参数错误,请确认!');history.back();</script>")
  Response.End
  End Select
End Sub


'**************************************************
'函数名:sub_Page
'作  用:显示“上一页 下一页”等信息
'参  数:Page_Num  ----当前页数
'       Page_Url  ----链接地址
'       Page_Count  ----总页数量
'       Record_Count ----显示总数量
'       Page_Size  ----每页数量
'       Page_Style  ----分页样式
'
'返回值:“上一页 下一页”等信息的HTML代码
'=============提供翻页数样式1===============
'**************************************************
Sub Sub_Page(Page_Num,Page_Url,Page_Count,Record_Count,Page_Size,Page_Style)
 Dim EndPage,p,i
 Select Case Page_Style
  Case 0
   Response.write ""
  Case 1  '样式一
   response.write "<table width=98% border=0 align=center cellpadding=0 cellspacing=0 style='font:3pt;'>"
   response.write "<tr align=center><td colspan='2' height='5'></td></tr>"
   response.write "<tr align=center><td width='50%'><a href='"&Page_Url&"page="&Page_Num&"'>刷新</a>&nbsp;&nbsp;页次:"
   response.write "<b><font color='red'>"&Page_Num&"</font></b>/<b>"&Page_Count&"</b>页&nbsp;每页<b>"& Page_Size &"</b> 总数<b>"&Record_Count&"</b></td><td width='50%'>分页:"
   if Page_Num > 4 then
    response.write "<a href="&Page_Url&"page=1>[1]</a> ..."
   end if
   if Page_Count>Page_Num+3 then
    endpage=Page_Num+3
   else
    endpage=Page_Count
   end if
   for i=Page_Num -3 to endpage
    if not i<1 then
     if i = clng(Page_Num) then
      response.write " <a href="&Page_Url&"page="&i&">["&i&"]</a>"
     else
      response.write " <a href="&Page_Url&"page="&i&">["&i&"]</a>"
     end if
    end if
   next
   if Page_Num+3 < Page_Count then
    response.write "... <a href="&Page_Url&"page="&Page_Count&">["&Page_Count&"]</a>"
   end if
   response.write "</td></tr>"
   response.write "<tr align=center><td colspan='2' height='5'></td></tr></table>"
  Case 2  '样式二
   response.write "<table width=98% border=0 align=center cellpadding=0 cellspacing=0 style='font:9pt;'>"
   response.write "<tr align=center><td height='2'></td></tr>"
   response.write "<tr align=center><td><a href='"&Page_Url&"page="&Page_Num&"'>刷新</a>&nbsp;"
   response.write "<a href="&Page_Url&"page=1>首页</a>&nbsp;"
   if Page_Num<2 then
    response.write "上一页&nbsp;"
    'response.write ""
   else
    response.write "<a href="&Page_Url&"page="&Page_Num-1&">上一页</a>&nbsp;"
   end if
   if Page_Count-Page_Num<1 then
    response.write "下一页&nbsp;"
    'response.write ""
   else
    response.write "<a href="&Page_Url&"page="&(Page_Num+1)&">下一页</a>&nbsp;"
   end if
   response.write "<a href="&Page_Url&"page="&Page_Count&">尾页</a>&nbsp;"
   response.write "页次:<b><font color='red'>"&Page_Num&"</font></b>/<b>"&page_count&"</b>页&nbsp;"
   response.write "共<b>"&Record_Count&"</b>条信息&nbsp;<B>"&Page_Size&"</B>条/页&nbsp;跳转"

   response.write "<select name='select' οnchange='javascript:window.location.href=this.options[this.selectedIndex].value'>"
   for p=1 to Page_Count
    response.write "<option "
    if cint(Page_Num)=p then response.write " selected "
    response.write "value='"&Page_Url&"page="&p&"'>"&p&"</option>"
   next
   response.write "</select>页"
   response.write "</td></tr></table>"
  Case 3  '样式三
   response.write "<table width=98% border=0 align=center cellpadding=0 cellspacing=0 style='font:9pt;'>"
   response.write "<tr align=center><td height='2'></td></tr>"
   response.write "<tr align=center><td><a href='"&Page_Url&"page="&Page_Num&"'>刷新</a>&nbsp;&nbsp;"
   response.write "<a href="&Page_Url&"page=1>首页</a>&nbsp;"
   if Page_Num<2 then
    response.write "上一页&nbsp;"
    'response.write ""
   else
    response.write "<a href="&Page_Url&"page="&Page_Num-1&">上一页</a>&nbsp;"
   end if
   if page_count-Page_Num<1 then
    response.write "下一页&nbsp;"
    'response.write ""
   else
    response.write "<a href="&Page_Url&"page="&(Page_Num+1)&">下一页</a>&nbsp;"
   end if
   response.write "<a href="&Page_Url&"page="&page_count&">尾页</a>&nbsp;"
   response.write "页次:<B><Font color='red'>"&Page_Num&"</Font></B>/<B>"&page_count&"</B>页&nbsp;"
   response.write "共有<b>"&Record_Count&"</b>条信息&nbsp;<B>"&Page_Size&"</B>条信息/页&nbsp;&nbsp;"
   response.write " 转到第<input type='text' name='page' size='3' maxlength='3'  value='" & Page_Num & "'  onKeyPress=""if (event.keyCode==13) window.location='" & Page_Url & "page=" & "'+this.value;""'>页" 
   response.write "&nbsp;<INPUT TYPE='submit' value='GO' onKeyPress=""if (event.keyCode==13) window.location='" & Page_Url & "page=" & "'+document.all.page.value;""' onMouseDown=""window.location='" & Page_Url & "page=" & "'+document.all.page.value;""'"">"
   response.write "</td></tr></table>"
  Case else
   response.write "<Div><Font Color='Red'>请选择正确的分页的样式!</Font></Div>"
 End Select
End Sub

 

%> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值