function verifyDate(textObj){
var tmpDateValue = textObj.value;
var tmpLength = tmpDateValue.length;
if (tmpLength == 0){
return true;
}
for (var i = 0; i < tmpLength;i++){
aChar = tmpDateValue.substring(i,i+1);
if(aChar != "-" && (aChar < "0" ¦¦ aChar > "9")) {
alert ("请按照格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if ((tmpLength < 8 ¦¦ tmpLength > 10) && tmpLength != 0) {
alert ("请按照格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
for (var j= 0; j < 4;j++){
aChar = tmpDateValue.substring(j,j+1);
if(aChar < "0" ¦¦ aChar > "9") {
alert ("请按照格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if (tmpDateValue.substring(4,5) != "-" ¦¦ tmpDateValue.substring(5,6) == "-"){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
if (tmpLength == 8){
if (tmpDateValue.substring(6,7) != "-" ¦¦ tmpDateValue.substring(7,8) == "-" ){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if (tmpLength == 9){
if (tmpDateValue.substring(8,9) == "-" ){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
if (tmpLength == 10){
if (tmpDateValue.substring(7,8) != "-" ¦¦ tmpDateValue.substring(6,7) == "-" ¦¦ tmpDateValue.substring(8,9) == "-" ¦¦ tmpDateValue.substring(9,10) == "-" ){
alert ("请按规定格式输入日期(yyyy-mm-dd)。");
textObj.focus(this);
textObj.select(this);
return false;
}
}
var count=0;
for (var k = 0; k < tmpLength;k++){
aChar = tmpDateValue.substring(k,k+1);
if(aChar == "-") {
count++;
}
}
if (count!=2){
alert("请按照格式输入日期!(yyyy-mm-dd)");
textObj.focus(this);
textObj.select(this);
return false;
}
return true;
}
上面的是校验输入框内是否是日期,且格式是由数字和"-"符号构成;符合"yyyy-m-d"或"yyyy-m-dd"或"yyyy-mm-d"或"yyyy-mm-dd"的格式(即textObj.value.length为8、9、10中的一种)function isFloat(textObj){
var tmpFloatValue = textObj.value;
var tmpLength = tmpFloatValue.length;
var count = 0;
for (var i = 0; i < tmpLength; i++){
aChar = tmpFloatValue.substring(i,i+1);
if (aChar != "." && !isNumber(aChar)){
alert("请输入合法数字!");
textObj.focus(this);
textObj.select(this);
return false;
}
}
for (var i = 0; i < tmpLength; i++){
aChar = tmpFloatValue.substring(i,i+1);
count = 0;
if (aChar == "."){
count++;
}
}
if (count > 1){
alert("请输入正确的浮点数!");
textObj.focus(this);
textObj.select(this);
return false;
}
return true;
}
上面的是校验输入框中输入的是否是数字
<script>
function isDate(tmpstr,tmpQuote)
{
var tmparr=tmpstr.split(tmpQuote)
tmparr[1]=tmparr[1]-1;//js中月份为0-11
tmpstr=tmparr.join("/");
var tmpdate=new Date(tmpstr)
if (isNaN(Date.parse(tmpdate))) return false;if ((tmpdate.getFullYear()!==tmparr[0]) ||(tmpdate.getMonth()!==tmparr[1]) ||(tmpdate.getDay()!==tmparr[2]))
return false;
}
</script>
function isDate(value){
var reg2=/^\d{4}-((0?[1-9])|(1[0-2]))-((0?[1-9])|([12][0-9])|(3[0-1]))$/;
return (reg1.test(value));
}
PS: 对于二月份的日期没有作更细致的检查