分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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:xhunanpp@hotmail.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有