Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Validating comma-delimited list for numeric entries

Contributor ,
Dec 16, 2014 Dec 16, 2014

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!

1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 16, 2014 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2014 Dec 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 18, 2014 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 18, 2014 Dec 18, 2014

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 18, 2014 Dec 18, 2014

Thanks, that one look like it'll work really well too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 17, 2014 Dec 17, 2014

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Dec 19, 2014 Dec 19, 2014

You could also do a simple regular expression check:

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

<--- good --->

<cfelse>

<!--- bad --->

</cfif>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 19, 2014 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2014 Dec 19, 2014
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources