javaScript通用数据类型校验函数(6)
来源:asp之家 时间:2009-07-06 12:49:00
功能:校验的格式为yyyy年MM月dd日HH时mm分ss秒,yyyy-MM-dd HH:mm:ss,yyyy/MM/dd HH:mm:ss,yyyyMMddHHmmss
提示信息:未输入或输入的时间格式错误
使用:f_check_time(obj)
返回:bool
function f_check_time(obj)
{
var time = Trim(obj.value);
var dtype = obj.eos_datatype;
var format = dtype.substring(dtype.indexOf("(")+1,dtype.indexOf(")")); //日期格式
var datePat,matchArray,year,month,day,hour,minute,second;
if(/^(y{4})(-|\/)(M{1,2})\2(d{1,2}) (HH:mm:ss)$/.test(format))
datePat = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
else if(/^(y{4})(年)(M{1,2})(月)(d{1,2})(日)(HH时mm分ss秒)$/.test(format))
datePat = /^(\d{4})年(\d{1,2})月(\d{1,2})日(\d{1,2})时(\d{1,2})分(\d{1,2})秒$/;
else if(format == "yyyyMMddHHmmss")
datePat = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
else
{
f_alert(obj,"日期格式不对");
return false;
}
matchArray = time.match(datePat);
if(matchArray == null)
{
f_alert(obj,"日期长度不对,或日期中有非数字符号");
return false;
}
if(/^(y{4})(-|\/)(M{1,2})\2(d{1,2}) (HH:mm:ss)$/.test(format))
{
year = matchArray[1];
month = matchArray[3];
day = matchArray[4];
hour = matchArray[5];
minute = matchArray[6];
second = matchArray[7];
} else
{
year = matchArray[1];
month = matchArray[2];
day = matchArray[3];
hour = matchArray[4];
minute = matchArray[5];
second = matchArray[6];
}
if (month < 1 || month > 12)
{
f_alert(obj,"月份应该为1到12的整数");
return false;
}
if (day < 1 || day > 31)
{
f_alert(obj,"每个月的天数应该为1到31的整数");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
f_alert(obj,"该月不存在31号");
return false;
}
if (month==2)
{
var isleap=(year % 4==0 && (year % 100 !=0 || year % 400==0));
if (day>29)
{
f_alert(obj,"2月最多有29天");
return false;
}
if ((day==29) && (!isleap))
{
f_alert(obj,"闰年2月才有29天");
return false;
}
}
if(hour<0 || hour>23)
{
f_alert(obj,"小时应该是0到23的整数");
return false;
}
if(minute<0 || minute>59)
{
f_alert(obj,"分应该是0到59的整数");
return false;
}
if(second<0 || second>59)
{
f_alert(obj,"秒应该是0到59的整数");
return false;
}
return true;
}
判断当前对象是否可见
function isVisible(obj){
var visAtt,disAtt;
try{
disAtt=obj.style.display;
visAtt=obj.style.visibility;
}catch(e){}
if(disAtt=="none" || visAtt=="hidden")
return false;
return true;
}