用JavaScript创建格式屏蔽

ZDNet软件频道 时间:2003-02-26 作者:Techmail 代码页 |  我要评论()
本文关键词:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--

String.prototype.trim = function () {
    return this.replace(/^s*/, "").replace(/s*$/, "");
}

String.prototype.padL = function (nLength, sChar) {
  var sreturn = this;
  while (sreturn.length < nLength) {
    sreturn = String(sChar) + sreturn;
  }
  return sreturn;
}
              
function date_onkeydown() {
  if (window.event.srcElement.readOnly) return;
  var key_code = window.event.keyCode;
  var oElement = window.event.srcElement;
  if (window.event.shiftKey && String.fromCharCode(key_code) == "T") {
        var d = new Date();
        oElement.value = String(d.getMonth() + 1).padL(2, "0") + "/" +
                         String(d.getDate()).padL(2, "0") + "/" +
                         d.getFullYear();
        window.event.returnValue = 0;
    }
    if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
        if ((key_code > 47 && key_code < 58) ||
          (key_code > 95 && key_code < 106)) {
            if (key_code > 95) key_code -= (95-47);
            oElement.value =
                oElement.value.replace(/[mdy]/, String.fromCharCode(key_code));
        }
        if (key_code == 8) {
            if (!oElement.value.match(/^[mdy0-9]/[mdy0-9]/[mdy0-9]$/))
                oElement.value = "mm/dd/yyyy";
            oElement.value = oElement.value.replace(/([mdy/]*)[0-9]([mdy/]*)$/,
                function (, , ) {
                    var idx = oElement.value.search(/([mdy/]*)[0-9]([mdy/]*)$/);
                    if (idx >= 5) {
                        return + "y" + ;
                    } else if (idx >= 2) {
                        return + "d" + ;
                    } else {
                        return + "m" + ;
                    }
                } );
            window.event.returnValue = 0;
        }
    }
    if (key_code != 9) {
        event.returnValue = false;
    }
}

//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE="text" NAME="txtDate" ID="txtDate" SIZE="10"
  MAXLENGTH="10" onkeydown="date_onkeydown()" VALUE="mm/dd/yyyy">
</FORM>
</BODY>
</HTML>

百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134