﻿// 表单验证


//时间验证
function isDate (theStr) 
{
    var the1st = theStr.indexOf('-');
    var the2nd = theStr.lastIndexOf('-');

    if (the1st == the2nd) { return(false); }
    else {
    var y = theStr.substring(0,the1st);
    var m = theStr.substring(the1st+1,the2nd);
    var d = theStr.substring(the2nd+1,theStr.length);
    var maxDays = 31;

    if (isInt(m)==false || isInt(d)==false || isInt(y)==false) return(false);
    else if (y.length < 4) return(false);
    else if (!isBetween (m, 1, 12)) return(false); 
    else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
    else if (m==2) {
            if (y % 4 > 0) maxDays = 28;
            else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
            else maxDays = 29;
    }
    if (isBetween(d, 1, maxDays) == false) { return(false); }
    else { return(true); }
    }
}
// 判断数字是否在另两个数字之间
function isBetween (val, lo, hi) 
{
    if ((val < lo) || (val > hi)) 
    { return(false); }
    else
    { return(true); }
}
// 判断是否为整数
function isInt (theStr) 
{
    var flag = true;

    if (isEmpty(theStr)) 
    { 
        flag=false; 
    }
    else
    { 
        for (var i=0; i<theStr.length; i++) 
        {
            if (isDigit(theStr.substring(i,i+1)) == false) 
            {flag = false; break;}
        }
    }
    return(flag);
}
//校验是否全由数字组成
function isDigit(s)
{
    var patrn=/^[0-9]{1,20}$/;
    if (!patrn.exec(s)) return false
    return true
}
// 判断是否为空
function isEmpty (str) {
    if ((str==null)||(str.length==0)) return true;
    else return(false);
}
