截至2017-07-14,下面的腳本還存在不會驗證閏年閏月、大小月的情況,大小月、閏年、閏月只能用其他方式驗證!
var currentFormat="YYYY-MM-dd HH:ss.SSS";//輸入常見日期格式 currentFormat = currentFormat //優先替換特殊字符,因為后面替換的正則表達式中包含特殊字符 .replace(/\s/ig, "\\s") .replace(/\//ig, "\\/") .replace(/\\/ig, "\\") .replace(/\./ig, "\\.") .replace(/\-/ig, "\\-") .replace(/yyyy/ig, "[1-9]\\d{3}")//年份 .replace(/yy/ig, "\\d{2}")//年份 .replace(/HH/, "((0[1-9])|(1\\d)|(2[0-4]))")//小時 .replace(/MM/, "((0[1-9])|(1[0-2])|\\d)")//月份 .replace(/dd/,"((0[1-9])|([1-2]\\d)|(3[0-1]))") .replace(/mm/, "[0-5]\\d|\\d")//分鍾 .replace(/ss/, "([0-5]\\d|\\d)")//秒鍾 .replace(/SSS/, "\\d{1,3}")//毫秒 currentFormat ="^"+currentFormat+"$";//"^{0}$".format(currentFormat); new RegExp(currentFormat).test(this.value.trim());//測試輸入值