CFLib.org – Common Function Library Project

isNumericList(nList [, delimiter])

Last updated March 20, 2002
Download UDF

author

John J. Rice                                      John J. Rice

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

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:

<cfscript>
/**
* 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;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson