CFLib.org – Common Function Library Project

isNumericList(nList[, delimiter])

Last updated March 20, 2002

author

John J. Rice

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Checks if a list consists of numeric values only. Handy for validating list received through forms and urls. Nice for checking a potentially hazardous list before using it in SQL.

Return Values:
Returns a boolean.

Example:

<cfoutput>
<!--- example of a basic number list --->
#isNumericList("7,9,8,0")#<br>
<!--- example of a list with a pipe ('|') delimiter --->
#isNumericList("7|9|8|0", "|" )#<br>
<!--- example of an invalid number list    --->
#isNumericList("7,9,8,a")#<br>
</cfoutput>

Parameters:

Name Description Required
nList List to check. Yes
delimiter Delimiter for the list. Defaults to a comma. No

Full UDF Source:

/**
 * Checks if a list consists of numeric values only.
 * 
 * @param nList      List to check. 
 * @param delimiter      Delimiter for the list. Defaults to a comma. 
 * @return Returns a boolean. 
 * @author John J. Rice (john@johnjrice.com) 
 * @version 1, March 20, 2002 
 */
function isNumericList(nList) {
    var intIndex    = 0;
    var aryN        = arrayNew(1);
    var strDelim    = ",";

    /*    default list delimiter to a comma unless otherwise specified            */
    if (arrayLen(arguments) gte 2){
        strDelim    = arguments[2];
    }
  
    /*    faster to work with arrays vs lists                                        */
    aryN        = listToArray(nlist,strDelim);
    
    for (intIndex=1;intIndex LTE arrayLen(aryN);intIndex=incrementValue(intIndex)) {
        if (compare(val(aryN[intIndex]),aryN[intIndex])) {
            /*    hit a non-numeric list element, send the no-go back                */
            return false;
        }
    }
    /*    made it through the list at this point, send the ok back                */    
    return true;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson