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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson