Copy link to clipboard
Copied
Hi!
I need to validate a comma-delimited list to make sure all the list items are numeric. I could do a cfloop to loop through the list, then an IsValid() to check each entry, but that seems cumbersome, so I was wondering if there was a better way.
Thanks!
Copy link to clipboard
Copied
I had the idea to just do a replace() to get rid of all the commas and treat the value as one big number. It seems to work.
Copy link to clipboard
Copied
BreakawayPaul wrote:
I had the idea to just do a replace() to get rid of all the commas and treat the value as one big number. It seems to work.
True. That is a creative test. But what if the list begins with 0 or contains negative or decimal numbers?
If you must include those such eventualities, then you could extend the test to something like
<cftry>
<cfset myList="0,1,2,x">
<cfset maxNo=arrayMax(listToArray(myList))>
<!--- The rest of the business code goes here --->
<cfcatch type="expression">
<cfoutput>#cfcatch.Detail#</cfoutput>
</cfcatch>
</cftry>
Copy link to clipboard
Copied
Aside from the original problem, your reply was helpful because I've never before considered a try/catch around anything but a query. Now I have a whole new bunch of things to try it on.
As far as expecting zeros and negative values, I don't really, because these values come from a form. That said, the point of this is to validate the input, so anything non-numeric needs to be rejected. What I've done for now is to force cfsqltype of numeric, then use a try/catch to trap the error if it's not. I suppose both methods work about equally well.
Copy link to clipboard
Copied
You initially stated you only needed to validate if a string contained only numeric values which to me should return a true/false response. Restricting or filtering a string to a list of only numeric values can be done using a preexisting UDF without using CFTRY.
Use justNumericList from CFLib.org.
http://www.cflib.org/udf/justNumericList
<cfparam name="Form.NumericValues" default="">
<cfset OnlyNumericValues = justNumericList(Form.NumericValues)>
<cfif ListLen(OnlyNumericValues)>
<!--- Do whatever you need to do --->
</cfif>
Copy link to clipboard
Copied
Thanks, that one look like it'll work really well too.
Copy link to clipboard
Copied
I recommend using the CFLib isNumericList() UDF from 2002:
Copy link to clipboard
Copied
You could also do a simple regular expression check:
<cfif reFind("^\d+(,\d+)*$",mylist)>
<--- good --->
<cfelse>
<!--- bad --->
</cfif>
Copy link to clipboard
Copied
The regex is too simple in the case of decimal or negative values, but would probably work well with simple positive integers. The isNumericList UDF uses VAL and then compares each values. The justNumericList filters out non-numeric values so that only numeric values remain in the list.
Copy link to clipboard
Copied
@Jamo
Fltering out non-numerical values is undoutedly a commendable solution. However, it introduces new business logic into the situation.
The original poster had 2 requirements: 1) a means of validating a list by detecting any non-numerical list element in it; 2) a creative way of doing so, which involves fewer steps than looping through the list.