views:

167

answers:

3

I have to validate form values as integers.

I have tried something like this:

<cfloop collection="#form#">
 <cfif form.value eq int(form.value)>
  #form.value# is an integer
 </cfif>
</cfloop>

It works as long the user does not input comma as the decimal separator, which is the default way of doing this here in Germany.

I have to use CF MX 6.1.

+1  A: 

You may, if you like, desensitize the input first.

<cfset var comma = ",">
<cfset var period = ".">
<cfset form.value = replace(form.value, comma, period, "all")>

But, if all you need is to verify if a field is an integer, why don't you look at CFLib.org - IsInt ?

<cfscript>
/**
* Checks to see if a var is an integer.
* version 1.1 - mod by Raymond Camden
*
* @param varToCheck      Value you want to validate as an integer.
* @return Returns a Boolean.
* @author Nathan Dintenfass ([email protected])
* @version 1.1, April 10, 2002
*/
function isInt(varToCheck){
return isNumeric(varToCheck) and round(varToCheck) is vartoCheck;
}
</cfscript>
Henry
+4  A: 

It would also probably help to look into the Internatational functions that are available. LSParseNumber(), for instance.

Al Everett
Wow. I looked at the list of LS functions and completely overlooked that one.
Ben Doom
+1  A: 

Like Al Everett, I recommend using the locale specific functions:

<!--- actually *setting* the desired locale is mandatory for this to work --->
<cfset SetLocale("German (Standard)")>

<cfif CGI.REQUEST_METHOD eq "POST">
  <!--- loop the FieldNames list so only real posted values are handled --->
  <cfloop list="#FORM.FieldNames#" index="FieldName">
    <cfif LSIsNumeric(FORM[FieldName])>
      <cfset num = LSParseNumber(FORM[FieldName])>
      <!--- do stuff with #num# --->
    </cfif>
  </cfloop>
</cfif>
Tomalak