日期:2014-05-16 浏览次数:20331 次
<html> <script> function parseDate() { var rqObj = document.getElementById("rq"); var rqValue = rqObj.value; if(rqValue=="") return; var reg = /([〇一二三四五六七八九十]{4})年([〇一二三四五六七八九十]{1,3})月([〇一二三四五六七八九十]{1,3})日/g; rqObj.value = rqValue.replace(reg, function (date, year, month,day) { var rs=[]; if(year && year.length==4) { rs.push(parse(year)); } if(month && month.length>0) { if(month.length==3){month = month.substring(0,2);} rs.push(parse(month)); } if(day && day.length>0) { if(day.length==3){day = day.substring(0,2);} rs.push(parse(day)); } return rs.join('-'); } ); } function parse(s) { var obj={'〇':0,'一':1,'二':2,'三':3,'四':4,'五':5,'六':6,'七':7,'八':8,'九':9,'十':1}; var rs=[]; for(var i=0;i<s.length;i++){ rs.push(obj[s.charAt(i)]); } return rs.join(''); } </script> <body> <input type="text" name="rq" id="rq" value="" onchange="parseDate();"/> </html>
------解决方案--------------------
试一把。。。
document.getElementById('t').onblur = function() { var reg = /^([一二三四五六七八九][〇一二三四五六七八九]+)年([一二三四五六七八九]|十[一二]{0,1})月([一二三四五六七八九]|(十|二十)[一二三四五六七八九]{0,1}|三十(|一))日$/; var value = this.value.replace(/^\s+|\s+$/g, ''); var matches = value.match(reg); var date = []; var temp; if(matches) { for(var i = 1; i <= 3; i++) { temp = trans(matches[i]); if(temp) { date.push(temp); } } if(date.length == 3) { this.value = date.join('-'); } } } function trans(str) { var map = '〇一二三四五六七八九十'; return str.length == 0? '' : str.length == 1? '0' + map.indexOf(str) : str.length == 2? (/^十/.test(str)? '1' + map.indexOf(str.substr(1)) : map.indexOf(str.substr(0,1)) + '0') : str.length == 3? map.indexOf(str.substr(0,1)).toString() + map.indexOf(str.substr(2,1)).toString() : str.replace((new RegExp('\[' + map + ']', 'g')), function(a) {return map.indexOf(a)}); }