isNumericList(nList [, delimiter])
Last updated March 20, 2002
Version: 1 | Requires: ColdFusion 5 | 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:
<!--- 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 added
DeMoronize
3 hour(s) ago
Chris Carey added
readPropertiesFi...
1 day(s) ago
Randy Johnson added
lastDayofWeek
3 day(s) ago
Frank Marion added
sitemapPing
7 day(s) ago
Top Rated
QuickSort
Rated 5.0, 3 time(s)
indentXml
Rated 5.0, 3 time(s)
queryColumnsToSt...
Rated 5.0, 3 time(s)
generateSsccAsn
Rated 5.0, 3 time(s)