JavaScript校验函数库
//月份数组
arr_mon=new Array("1","2","3","4","5","6","7","8","9","10","11","12",
"01","02","03","04","05","06","07","08","09");
//判断字符串是否为合法非负整数
// a-1 ifDigit(str,allowNull)
function ifDigit(str,allowNull){
slen=str.length;
if(slen==0) return allowNull;
for (i=0; i<slen; i++){
cc = str.charAt(i);
if (cc <"0" || cc >"9"){
return false;
}
}
return true;
}
//功能:判断字符串是否都是英文字母
// a-2 ifLetter(str,allowNull)
function ifLetter(str,allowNull ){
slen=getLength(str);
if (slen==0) return allowNull;
str = str.toUpperCase();
for ( var i = 0 ; i < slen; i ++ ){
if ( str.charAt(i) < "A" || str.charAt(i) > "Z" )
return false;
}
return true;
}
//功能:判断某个元素是否存在于数组中
//a-3 ifExist(arr,element) arr:数组;element:某个元素
function ifExist(arr,element){
for(var i=0;i<arr.length;i++){
if(element==arr[i]) return true;
}
return false;
}
//功能:判断是否为合法天数
//a-4 ifDay(str,allowNull)
function ifDay(str,allowNull){
slen=getLength(str);
if (slen==0) return allowNull;
return ifExist(arr_day,str);
}
//功能:判断是否为合法月份
//a-5 ifMonth(str,allowNull)
function ifMonth(str,allowNull){
slen=getLength(str);
if (slen==0) return allowNull;
return ifExist(arr_mon,str);
}
//功能:判断是否为合法年份(要求是四位整数,且数字>=1800 并且<=2050)
//a-6 ifYear(str,allowNull)
function ifYear(str,allowNull){
return ifYearInRange(str,1800,2050,allowNull);
}
//功能:判断是否为合法年份(要求是四位整数,且数字>=min_year 并且<=max_year)
//a-7 ifYearInRange(str,min_year,max_year,allowNull)
function ifYearInRange(str,min_year,max_year,allowNull){
slen=getLength(str);
if (slen==0) return allowNull;
if (slen!=4) return false;
if (!ifDigit(str,allowNull)) return false;
if (parseInt(str)<min_year || parseInt(str)>max_year) return false;
return true;
}
//功能:判断是否为合法日期格式(要求是八位整数,格式为"yyyymmdd")
//a-8 ifDate(str,allowNull)
function ifDate(str,allowNull){
/*
slen=getLength(str);
if(slen==0) return allowNull;
if(slen!=8) return false;
if(!ifDigit(str,false)) return false;
var year = str.substr(0,4);
if(!ifYear(year,false)) return false;
var month= str.substr(4,2);
if(!ifMonth(month,false)) return false;
var day= str.substr(6,2);
if(!ifDay(day,false)) return false;
if(!ifAvildDate(year,month,day)) return false;
return true;
*/
/*slen=getLength(str);
if(slen==0) return allowNull;
if(slen!=10) return false;
var year = str.substr(0,4);
if(!ifYear(year,false)) return false;
var s = str.substr(4,1);
if(s!="-") return false;
var month= str.substr(5,2);
if(!ifMonth(month,false)) return false;
var s = str.substr(7,1);
if(s!="-") return false;
var day= str.substr(8,2);
if(!ifDay(day,false)) return false;
if(!ifAvildDate(year,month,day)) return false;
return true; */
slen=getLength(str);
if(slen==0) return allowNull;
if(slen<8 || slen>10) return false;
var year = str.substr(0,4);
if(!ifYear(year,false)) return false;
var s = str.substr(4,1);
if(s!="-") return false;
str = str.substr(5);
var pos = str.indexOf("-");
if(pos == -1) return false;
var month = str.substring(0,pos);
if(!ifMonth(month,false)) return false;
var day = str.substr(pos + 1);
if(!ifDay(day,false)) return false;
var yearint = parseInt(year);
var monthint = parseInt(month);
var dayint = parseInt(day);
if(yearint%4 == 0)
{
if(monthint==2 && dayint>29) return false;
}
else
{
if(monthint==2 && dayint>28) return false;
}
return true;
}