0

iframe に送信される ajax フォームがあります。サーバー側では、入力を有効な整数として検証し、検証されない場合はデフォルト値の 0 に設定します。最初の解決策は次のとおりです。

<cfparam name="FORM.integerField" default="0" type="integer">

ただし、フォームの送信時に文字列データが入力された場合、サーバーはエラーをスローします。エラーをスローしたくありません。データ型をチェックし、チェックに失敗した場合はデフォルトに設定してから、from の処理を​​続けたいと考えています。私も次のようなことを試しました:

<cfqueryparam value="#atest#" cfsqltype="CF_SQL_NUMERIC"> <!-- inside my query -->

<cfif IsDefined(LSParseNumber(FORM.integerField))>

<cfif LSParseNumber(FORM.integerField)>

助言がありますか?

4

2 に答える 2

3
<cfif NOT isValid("integer",FORM.integerField)>
    <cfset FORM.integerField = 0>
</cfif>

I <3 "isValid()"。

于 2012-08-08T13:18:02.097 に答える
0

これはあなたが望むものにはやり過ぎかもしれませんが、それは仕事をします. number 引数を指定するだけです。

<cffunction name="CastAsInteger" access="public" returntype="String" output="false" hint="Returns the provided number if it matches the bits and sign, otherwise returns 0">
    <cfargument name="number" type="any" required="true" hint="Any string that may be an integer">
    <cfargument name="bits" type="numeric" default="32" required="false" hint="Number of bits to determine the max size of the integer">
    <cfargument name="signed" type="boolean" default="true" required="false" hint="If the integer is signed or unsigned">

    <cfset local.ReturnInt = Arguments.Number>

    <!--- Make sure it's just a number --->
    <cfif REFind("^-?[1-9]+[0-9]*$", local.ReturnInt) EQ false>
        <cfset local.ReturnInt = 0>
    </cfif>

    <!--- Check size --->
    <cfif Arguments.signed EQ true>
        <cfif local.ReturnInt LT PrecisionEvaluate(-(2^(Arguments.bits - 1))) OR local.ReturnInt GT PrecisionEvaluate((2^(Arguments.bits - 1)) - 1)>
            <cfset local.ReturnInt = 0>
        </cfif>
    <cfelse>
        <cfif local.ReturnInt LT 0 OR local.ReturnInt GT PrecisionEvaluate((2^Arguments.bits) - 1)>
            <cfset local.ReturnInt = 0>
        </cfif>
    </cfif>

    <cfreturn local.ReturnInt>
</cffunction>
于 2012-08-08T14:09:38.353 に答える