2009-07-19 25 views
2

我必須驗證表單值爲整數。如何在CF中處理以逗號分隔的小數和浮點數?

我已經試過這樣的事情:

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

它的工作原理,只要用戶不輸入逗號作爲小數點分隔符,這是德國在這裏做這個的默認方式。我不得不使用CF MX 6.1。

回答

4

這也可能有助於查看可用的國際功能。例如,LSParseNumber()

+0

哇。我查看了LS功能列表,並完全忽略了這一點。 – 2009-07-20 14:22:50

1

如果您願意,您可以首先對輸入敏感。

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

但是,如果你需要的是驗證一個字段是一個整數,你爲什麼不看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> 
1

像鋁埃弗雷特,我建議使用區域設置特定的功能:

<!--- 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> 
相關問題