一个完美的日期控件 + 一个用脚本断判日期型的方法(2)

类别:.NET开发 点击:0 评论:0 推荐:

1. 一个类似于Calendar日期控件,不用再刷新页面。
2. 判断文本控件里的值是否能转换成日期型。

.htc 例1:
/*
 *xpMask.htc
 *
 */

//------------------------------------------------------------------------------------------------------

<PUBLIC:COMPONENT
 lightWeight=false
>

<PUBLIC:DEFAULTS
 contentEditable=false
 tabStop=true
/>

<PUBLIC:attach event="ondocumentready" onevent="initCoolMask()" />
<PUBLIC:attach event="ondetach" onevent="cleanupCoolMask()" />

<PUBLIC:property name="maskType" value="" />
<PUBLIC:property name="realValue" value="" />
<PUBLIC:property name="toolTipStr" value="" />

<script language="VBScript">


sub initCoolMask()
 attachEvent "onreadystatechange", GetRef("coolMaskInputBlur")
 attachEvent "onfocus", GetRef("coolMaskInputFocus")
 attachEvent "onblur", GetRef("coolMaskInputBlur")
 coolMaskInputBlur
end sub

sub cleanupCoolMask()
 detachEvent "onreadystatechange", GetRef("coolMaskInputBlur")
 detachEvent "onfocus", GetRef("coolMaskInputFocus")
 detachEvent "onblur", GetRef("coolMaskInputBlur")
end sub

sub coolMaskInputFocus()
 with element
  if not .realValue = "" then .value = .realValue
  .select()
 end with
end sub

sub coolMaskInputBlur()
 with element
  select case ucase(.maskType)
      case "DATETIME"
          .realValue = .value
          .value = maskDatetime(.value)
          .toolTipStr = .ToolTip
      case "SHORTDATE"
    .realValue = .value
    if maskDate(.value, "short") = formatDateTime("1900-1-1 0:00:00", vbShortDate) then
        .value = ""
        .toolTipStr = "Format: yyyy-mm-dd "
    else
        .value = maskDate(.value, "short")  
        if not .realValue = "" then 
            .toolTipStr = "Format: "&.realValue
        else         
            .toolTipStr = "Format: yyyy-mm-dd "
        end if
    end if
    .title =.toolTipStr
      case "MEDIUMDATE"
    .realValue = .value
          .value = maskDate(.value, "medium")
      case "LONGDATE"
    .realValue = .value
          .value = maskDate(.value, "long")
      case "ZIPCODE"
    .realValue = parseChar(.value, array(" ", "-"))
    .value = maskZip(.value)     
      case "PHONE"
    .realValue = parseChar(.value, array(" ", "(", ")", "-", "."))
          .value = maskPhone(.value)
      case "PERCENT"
    .realValue = parseChar(.value, array(" ", "%"))
          .value = maskPercent(.value)
      case else
    .realValue = .value
  end select
 end with
end sub

function parseChar(sStr, sChar)
    dim i, zChar, sNewStr
    if typeName(sChar) = "string" then zChar = Array(sChar) else zChar = sChar
    sNewStr = sStr
 for i = lBound(zChar) to uBound(zChar)
  sNewStr = replace(sNewStr, cstr(zChar(i)), "")
    next
    parseChar = sNewStr
end function

function setViewState(bState)
 with element
  if not bState then
   .runtimeStyle.color = .style.color
  else
   .runtimeStyle.color = "red"
  end if
 end with
end function

function maskDate(sValue, sType)
    if IsNumeric(sValue) then
  'sValue = parseChar(sValue, array(" ", "-", "/", ",", ".", "\", "^", "&", "*", "@", "~", "`", "'", "!", "#", "$", "%", "|", "(", ")", "+", "_", "=", ";", "?", ":", "{", "}", "[", "]", "<", ">")) 
  if len(sValue) = 8 then sValue = left(sValue, 4) & "-" & left(right(sValue, 4), 2) & "-" & right(right(sValue, 4), 2)
  if len(sValue) = 6 then sValue = left(sValue, 4) & "-0" & left(right(sValue, 2), 1) & "-0" & right(right(sValue, 2), 1)
 end if
 dim zMonth
 zMonth = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
 if len(trim(sValue)) = 0 then
  maskDate = ""
  setViewState false
 elseif not(isDate(sValue)) then
  maskDate = "DATE ERROR"
  setViewState true
 else
  select case (sType)
      case "medium"
          maskDate = day(dateValue(sValue)) & "-" & left(zMonth(month(dateValue(sValue)) - 1), 3) & "-" & year(dateValue(sValue))
      case "long"
          maskDate = zMonth(month(dateValue(sValue)) - 1) & " " & day(dateValue(sValue)) & ", " & year(dateValue(sValue))
      case else
          maskDate = formatDateTime(sValue, vbShortDate)
  end select
  setViewState false
 end if
end function

function maskDatetime(sValue)
    dim sNewValue   
    sNewValue = parseChar(sValue, array(" ", "-", "/", ",", ".", "\", "^", "&", "*", "@", "~", "`", "'", "!", "#", "$", "%", "|", "(", ")", "+", "_", "=", ";", "?", ":", "{", "}", "[", "]", "<", ">"))
 if len(sNewValue) = 0 then
  maskDatetime = ""
  setViewState false
 elseif (len(sNewValue) <> 8 and len(sNewValue) <> 7 and len(sNewValue) <> 6) or not isnumeric(sNewValue) then
  maskDatetime = "DATE ERROR"
  setViewState true
 else
  if len(sNewValue) = 8 then sNewValue = left(sNewValue, 4) & "-" & left(right(sNewValue, 4), 2) & "-" & right(right(sNewValue, 4), 2)
  if len(sNewValue) = 7 then
     if IsNumeric(right(sValue, 2)) then
         sNewValue = left(sNewValue, 4) & "-0" & left(right(sNewValue, 3), 1) & "-" & right(right(sNewValue, 3), 2)
     else
         sNewValue = left(sNewValue, 4) & "-" & left(right(sNewValue, 3), 2) & "-0" & right(right(sNewValue, 3), 1)
     end if
  end if
  if len(sNewValue) = 6 then sNewValue = left(sNewValue, 4) & "-0" & left(right(sNewValue, 2), 1) & "-0" & right(right(sNewValue, 2), 1)
  if not(isDate(sNewValue)) then
   maskDatetime = "DATE ERROR"
   setViewState true
  else
      'sNewValue = parseChar(sNewValue, array(" ", "-", "/", ",", "."))
   maskDatetime = FormatDateTime(sNewValue, 2)
   setViewState false
  end if
 end if
end function

function maskZip(sValue)
 dim sNewValue
    sNewValue = parseChar(sValue, array(" ", "-"))
 if len(sNewValue) = 0 then
  maskZip = ""
  setViewState false
 elseif (len(sNewValue) <> 5 and len(sNewValue) <> 9) or not isnumeric(sNewValue) then
  maskZip = "ZIPCODE ERROR"
  setViewState true
 else
  if len(sNewValue) = 9 then sNewValue = left(sNewValue, 5) & "-" & right(sNewValue, 4)
  maskZip = sNewValue
  setViewState false
 end if
end function

function maskPhone(sValue)
 dim sNewValue
    sNewValue = parseChar(sValue, array(" ", "(", ")", "-", "."))
 if len(sNewValue) = 0 then
  maskPhone = ""
  setViewState false
 elseif (len(sNewValue) <> 7 and len(sNewValue) <> 10) or not isnumeric(sNewValue) then
  maskPhone = "PHONE ERROR"
  setViewState true
 else
  select case len(sNewValue)
   case 7
    maskPhone = left(sNewValue, 3) & "-" & right(sNewValue, 4)
   case 10
    maskPhone = "(" & left(sNewValue, 3) & ") " & mid(sNewValue, 4, 3) & "-" & right(sNewValue, 4)
  end select
  setViewState false
 end if
end function

function maskPercent(sValue)
 dim sNewValue
    sNewValue = parseChar(sValue, array(" ", "%"))
 if len(sNewValue) = 0 then
  maskPercent = ""
  setViewState false
 else
  on error resume next
  err.clear
  maskPercent = formatPercent(sNewValue)
  if err.number = 13 then
      on error goto 0
   maskPercent = "PERCENT ERROR"
   setViewState true
   exit function
  end if
  on error goto 0
  setViewState false
 end if
end function

</script>

</PUBLIC:COMPONENT>


//------------------------------------------------------------------------------------------------------

.css 例1:
/*
 *xpText.css
 *
 */

.coolMask
{
 FONT-SIZE: 10pt;
 BEHAVIOR: url(../Htc/coolMask.htc); 
 FONT-FAMILY: Verdana, Arial, Helvetica;
}

//------------------------------------------------------------------------------------------------------

页面调用:

<asp:textbox id="birthday" CssClass="coolMask" ondblclick="javascript: setday(this);" maskType="shortDate" runat="server" Width="106px"></asp:textbox>

注意:
CssClass="coolMask" 这个不用说了,大家都知道是什么
ondblclick="javascript: setday(this);"    这是双激事件
maskType="shortDate"       maskType属性在coolWindowsCalendar.js中文定义了,
maskType属性类型一共有如下几种:
DATETIME :日期
SHORTDATE :日期
MEDIUMDATE :日期
LONGDATE :日期
PHONE :电话号码
PERCENT :百分数

realValue="" : realValue属性在coolWindowsCalendar.js中文定义了,
realValue : 用来保存当前值,( element.realValue = textbox.value)

toolTipStr : toolTipStr 属性在coolWindowsCalendar.js中文定义了,
toolTipStr : 用来显示格式, 就是title提示框,

查看地址:http://print.itgaga.org/PrintERP/OrderManage/Bus_Invoice_EFrame.aspx(填写日期)

源代码:data.rar

(完)第一次发表文章,失敬```
 QQ:5331682
 MSN:[email protected]

本文地址:http://com.8s8s.com/it/it44927.htm