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

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

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

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

  1<public:component lightweight="false">
  2<public:defaults contenteditable="false" tabstop="true"></public:defaults>
  3<public:attach event="ondocumentready" onevent="initCoolMask()"></public:attach>
  4<public:attach event="ondetach" onevent="cleanupCoolMask()"></public:attach>
  5<public:property name="maskType" value=""></public:property>
  6<public:property name="realValue" value=""></public:property>
  7<public:property name="toolTipStr" value=""></public:property>
  8<script language="VBScript">
  9
 10  
 11sub initCoolMask()   
 12attachEvent "onreadystatechange", GetRef("coolMaskInputBlur")   
 13attachEvent "onfocus", GetRef("coolMaskInputFocus")   
 14attachEvent "onblur", GetRef("coolMaskInputBlur")   
 15coolMaskInputBlur   
 16end sub 
 17
 18sub cleanupCoolMask()   
 19detachEvent "onreadystatechange", GetRef("coolMaskInputBlur")   
 20detachEvent "onfocus", GetRef("coolMaskInputFocus")   
 21detachEvent "onblur", GetRef("coolMaskInputBlur")   
 22end sub 
 23
 24sub coolMaskInputFocus()   
 25with element   
 26if not .realValue = "" then .value = .realValue   
 27.select()   
 28end with   
 29end sub 
 30
 31sub coolMaskInputBlur()   
 32with element   
 33select case ucase(.maskType)   
 34case "DATETIME"   
 35.realValue = .value   
 36.value = maskDatetime(.value)   
 37.toolTipStr = .ToolTip   
 38case "SHORTDATE"   
 39.realValue = .value   
 40if maskDate(.value, "short") = formatDateTime("1900-1-1 0:00:00", vbShortDate) then   
 41.value = ""   
 42.toolTipStr = "Format: yyyy-mm-dd "   
 43else   
 44.value = maskDate(.value, "short")   
 45if not .realValue = "" then   
 46.toolTipStr = "Format: "&.realValue   
 47else   
 48.toolTipStr = "Format: yyyy-mm-dd "   
 49end if   
 50end if   
 51.title =.toolTipStr   
 52case "MEDIUMDATE"   
 53.realValue = .value   
 54.value = maskDate(.value, "medium")   
 55case "LONGDATE"   
 56.realValue = .value   
 57.value = maskDate(.value, "long")   
 58case "ZIPCODE"   
 59.realValue = parseChar(.value, array(" ", "-"))   
 60.value = maskZip(.value)   
 61case "PHONE"   
 62.realValue = parseChar(.value, array(" ", "(", ")", "-", "."))   
 63.value = maskPhone(.value)   
 64case "PERCENT"   
 65.realValue = parseChar(.value, array(" ", "%"))   
 66.value = maskPercent(.value)   
 67case else   
 68.realValue = .value   
 69end select   
 70end with   
 71end sub 
 72
 73function parseChar(sStr, sChar)   
 74dim i, zChar, sNewStr   
 75if typeName(sChar) = "string" then zChar = Array(sChar) else zChar = sChar   
 76sNewStr = sStr   
 77for i = lBound(zChar) to uBound(zChar)   
 78sNewStr = replace(sNewStr, cstr(zChar(i)), "")   
 79next   
 80parseChar = sNewStr   
 81end function 
 82
 83function setViewState(bState)   
 84with element   
 85if not bState then   
 86.runtimeStyle.color = .style.color   
 87else   
 88.runtimeStyle.color = "red"   
 89end if   
 90end with   
 91end function 
 92
 93function maskDate(sValue, sType)   
 94if IsNumeric(sValue) then   
 95'sValue = parseChar(sValue, array(" ", "-", "/", ",", ".", "\", "^", "&", "*", "@", "~", "`", "'", "!", "#", "$", "%", "|", "(", ")", "+", "_", "=", ";", "?", ":", "{", "}", "[", "]", "<", ">"))   
 96if len(sValue) = 8 then sValue = left(sValue, 4) & "-" & left(right(sValue, 4), 2) & "-" & right(right(sValue, 4), 2)   
 97if len(sValue) = 6 then sValue = left(sValue, 4) & "-0" & left(right(sValue, 2), 1) & "-0" & right(right(sValue, 2), 1)   
 98end if   
 99dim zMonth   
100zMonth = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")   
101if len(trim(sValue)) = 0 then   
102maskDate = ""   
103setViewState false   
104elseif not(isDate(sValue)) then   
105maskDate = "DATE ERROR"   
106setViewState true   
107else   
108select case (sType)   
109case "medium"   
110maskDate = day(dateValue(sValue)) & "-" & left(zMonth(month(dateValue(sValue)) - 1), 3) & "-" & year(dateValue(sValue))   
111case "long"   
112maskDate = zMonth(month(dateValue(sValue)) - 1) & " " & day(dateValue(sValue)) & ", " & year(dateValue(sValue))   
113case else   
114maskDate = formatDateTime(sValue, vbShortDate)   
115end select   
116setViewState false   
117end if   
118end function 
119
120function maskDatetime(sValue)   
121dim sNewValue   
122sNewValue = parseChar(sValue, array(" ", "-", "/", ",", ".", "\", "^", "&", "*", "@", "~", "`", "'", "!", "#", "$", "%", "|", "(", ")", "+", "_", "=", ";", "?", ":", "{", "}", "[", "]", "<", ">"))   
123if len(sNewValue) = 0 then   
124maskDatetime = ""   
125setViewState false   
126elseif (len(sNewValue) <> 8 and len(sNewValue) <> 7 and len(sNewValue) <> 6) or not isnumeric(sNewValue) then   
127maskDatetime = "DATE ERROR"   
128setViewState true   
129else   
130if len(sNewValue) = 8 then sNewValue = left(sNewValue, 4) & "-" & left(right(sNewValue, 4), 2) & "-" & right(right(sNewValue, 4), 2)   
131if len(sNewValue) = 7 then   
132if IsNumeric(right(sValue, 2)) then   
133sNewValue = left(sNewValue, 4) & "-0" & left(right(sNewValue, 3), 1) & "-" & right(right(sNewValue, 3), 2)   
134else   
135sNewValue = left(sNewValue, 4) & "-" & left(right(sNewValue, 3), 2) & "-0" & right(right(sNewValue, 3), 1)   
136end if   
137end if   
138if len(sNewValue) = 6 then sNewValue = left(sNewValue, 4) & "-0" & left(right(sNewValue, 2), 1) & "-0" & right(right(sNewValue, 2), 1)   
139if not(isDate(sNewValue)) then   
140maskDatetime = "DATE ERROR"   
141setViewState true   
142else   
143'sNewValue = parseChar(sNewValue, array(" ", "-", "/", ",", "."))   
144maskDatetime = FormatDateTime(sNewValue, 2)   
145setViewState false   
146end if   
147end if   
148end function 
149
150function maskZip(sValue)   
151dim sNewValue   
152sNewValue = parseChar(sValue, array(" ", "-"))   
153if len(sNewValue) = 0 then   
154maskZip = ""   
155setViewState false   
156elseif (len(sNewValue) <> 5 and len(sNewValue) <> 9) or not isnumeric(sNewValue) then   
157maskZip = "ZIPCODE ERROR"   
158setViewState true   
159else   
160if len(sNewValue) = 9 then sNewValue = left(sNewValue, 5) & "-" & right(sNewValue, 4)   
161maskZip = sNewValue   
162setViewState false   
163end if   
164end function 
165
166function maskPhone(sValue)   
167dim sNewValue   
168sNewValue = parseChar(sValue, array(" ", "(", ")", "-", "."))   
169if len(sNewValue) = 0 then   
170maskPhone = ""   
171setViewState false   
172elseif (len(sNewValue) <> 7 and len(sNewValue) <> 10) or not isnumeric(sNewValue) then   
173maskPhone = "PHONE ERROR"   
174setViewState true   
175else   
176select case len(sNewValue)   
177case 7   
178maskPhone = left(sNewValue, 3) & "-" & right(sNewValue, 4)   
179case 10   
180maskPhone = "(" & left(sNewValue, 3) & ") " & mid(sNewValue, 4, 3) & "-" & right(sNewValue, 4)   
181end select   
182setViewState false   
183end if   
184end function 
185
186function maskPercent(sValue)   
187dim sNewValue   
188sNewValue = parseChar(sValue, array(" ", "%"))   
189if len(sNewValue) = 0 then   
190maskPercent = ""   
191setViewState false   
192else   
193on error resume next   
194err.clear   
195maskPercent = formatPercent(sNewValue)   
196if err.number = 13 then   
197on error goto 0   
198maskPercent = "PERCENT ERROR"   
199setViewState true   
200exit function   
201end if   
202on error goto 0   
203setViewState false   
204end if   
205end function 
206
207</script>
208</public:component>

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

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

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

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

页面调用:

1<asp:textbox cssclass="coolMask" id="birthday" masktype="shortDate" ondblclick="javascript: setday(this);" 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]

Published At
Categories with Web编程
Tagged with
comments powered by Disqus