Why isvalid(“integer”,“1,5”) = YES?

偶尔善良 提交于 2019-11-29 06:08:50
Henry

Adobe is aware of this, but...

State: Closed

Status: Withdrawn

Reason: AsDesigned

https://bugbase.adobe.com/index.cfm?event=bug&id=3169196

TBH I'm a little embarrassed to be using a language that can't even validate integer correctly.

UPDATE:

Guess what, it'll be addressed in CF12!

There is no doubt that this behavior is incorrect. It is obviously wrong and it should be corrected. However, it has been like this forever and making such a fundamental change has a great potential to break a lot of applications. We dont want to do that in this release. As Rakshith has already communicated, we plan to take up such changes in 'Dazzle' where we will correct the behavior without worrying about backward compatibility.

http://blog.adamcameron.me/2014/02/can-we-please-agree-that-adobe-is-not.html

Just to expand on the answer here and explain what's actually going on.

See this related bug, a summary of the detail of which is that CF will interpret "m,n" (where m and n are digits) as a DATE if it possibly can. This is ludicrous: "m,n" is not a format that represents a date to anyone on the planet, so there is no reason for CF to ever interpret it this way.

Adobe copped-out of fixing this issue to, citing the same excuse that it would break backwards compat. Nonsense it would.

Anyway... because a date can be cast to an integer in CF, CF thinks "1,5" is a legit integer, because it's actually a date. How embrrassing it is - as a dedicated CF developer - to have to offer you than explanation. Sorry.

If you want to make absolutely sure you have an Integer, you could use Java Integer methods.

<cfscript>
createObject("java","java.lang.Integer").parseInt("1,5");
</cfscript>

The parseInt() method throws when it is given anything that cannot be interpreted as an Integer. This includes "1,5".

Server side validation (only need the cfif logic, loop for example only)

<cfloop list="2.123,a,4" index="myVal">
    <cfif !isNumeric(myVal)> ...error code for not numeric<br />
    <cfelseif myVal neq int(myVal)> ...error code for not integer<br />
    <cfelse>is integer<br />
    </cfif>
</cfloop>

Tighter direct code:

<cfif !isNumeric(myVal) and myVal neq int(myVal)> ...error code for not integer<br />
</cfif>

You could write a cffunction as well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!