常用javascript函数(一)


/*********************************************************************************
*    FUNCTION:        isBetween
*    PARAMETERS:        val        AS any value
*                    lo        AS Lower limit to check
*                    hi        AS Higher limit to check
*    CALLS:            NOTHING
*    RETURNS:        TRUE if val is between lo and hi both inclusive, otherwise false.
**********************************************************************************/
function isBetween (val, lo, hi) {
    if ((val < lo) || (val > hi)) { return(false); }
    else { return(true); }
}

/*********************************************************************************
*    FUNCTION:        isDate checks a valid date
*    PARAMETERS:        theStr        AS String
*    CALLS:            isBetween, isInt
*    RETURNS:        TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
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:        isEuDate checks a valid date in British format
*    PARAMETERS:        theStr        AS String
*    CALLS:            isBetween, isInt
*    RETURNS:        TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isEuDate (theStr) {
    if (isBetween(theStr.length, 8, 10) == false) { return(false); }
    else {
        var the1st = theStr.indexOf('/');
        var the2nd = theStr.lastIndexOf('/');
       
        if (the1st == the2nd) { return(false); }
        else {
            var m = theStr.substring(the1st+1,the2nd);
            var d = theStr.substring(0,the1st);
            var y = 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) == false) { 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:       Compare Date! Which is the latest!
*   PARAMETERS:     lessDate,moreDate AS String
*   CALLS:          isDate,isB

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3