我有一个提交给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)>
有什么建议吗?
答案 0 :(得分:3)
<cfif NOT isValid("integer",FORM.integerField)>
<cfset FORM.integerField = 0>
</cfif>
我&lt; 3“isValid()”。
答案 1 :(得分:0)
这对你想要的东西来说可能有些过分,但它会完成这项工作。只需提供数字参数。
<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>