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