Skip to main content
BreakawayPaul
Inspiring
December 16, 2014
Question

Validating comma-delimited list for numeric entries

  • December 16, 2014
  • 3 replies
  • 1410 views

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!

    This topic has been closed for replies.

    3 replies

    Legend
    December 19, 2014

    You could also do a simple regular expression check:

    <cfif reFind("^\d+(,\d+)*$",mylist)>

    <--- good --->

    <cfelse>

    <!--- bad --->

    </cfif>

    James Moberg
    Inspiring
    December 19, 2014

    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.

    BKBK
    Community Expert
    Community Expert
    December 19, 2014

    @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.

    James Moberg
    Inspiring
    December 17, 2014

    I recommend using the CFLib isNumericList() UDF from 2002:

    http://www.cflib.org/udf/isNumericList

    BreakawayPaul
    Inspiring
    December 16, 2014

    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.

    BKBK
    Community Expert
    Community Expert
    December 18, 2014

    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>

    BreakawayPaul
    Inspiring
    December 18, 2014

    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.