复制代码 代码如下: 日期输入 window.onload = function(){ var aLaydate = document.getElementsByClassName("tarikh"); for(var i = 0;i < aLaydate.length;i ) { aLaydate[i].onchange = function(){ var dateValue = this.value; dateValue = dateValue.replace(/。/g,"-"); dateValue = dateValue.replace(/./g,"-"); if(dateValue.length == 8){ var temp = dateValue.substring(0,4) "-" dateValue.substring(4,6) "-" dateValue.substring(6,8); dateValue = temp; console.log(dateValue); } if(CheckDT(dateValue)){ this.value = dateValue; } lain { makluman("日期输入错误"); } } } } fungsi CheckDT(str) { var r = str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/); jika(r==null) { kembali palsu; } lain { var d= Tarikh baharu(r[1], r[3]-1, r[4]); return (d.getFullYear()==r[1]&&(d.getMonth() 1)==r[3]&&d.getDate()==r[4]); } }