开发者

Why is this simple "If #stringLength# LT 10" comparison not working?

So I'm using ColdFusion to validate a form, and due to the nature of this form, the validation can be very simple. I've got it all working except for the telephone field. The only criteria that must be met for the input phone number to be valid is that it must be at least 10 characters long after all non-numeric characters have been stripped out. Below is the exact code I'm using:

<cfif len(REreplace(phoneNum, "[^0-9]", "", "all")) LT 10>
    [THROW AN ERROR]
</cfif>

The problem is that an error is thrown every time, no matter the length of phoneNum UNLESS I include a non-numeric character for REreplace() to replace. I wanted to see what was going on and try something a little different, so I experimented with this:

<cfset ph开发者_如何学运维oneLength = len(REreplace(phoneNum, "[^0-9]", "", "all"))>
<cfdump var="#phoneLength#">

<cfif phoneLength LT 10>
    [THROW AN ERROR]
</cfif>

In this case, I get a dumped value of 10 or more, but the error still gets thrown unless I put a non-numeric character in phoneNum. I can have the exact same number of numeric characters (i.e. the dumped value stays the same) but adding in parentheses or something changes whether or not an error is thrown. Does anyone know what's going on here? I can't believe I'm asking StackOverflow a question about If/Then's, but I just can't understand how ColdFusion can evaluate phoneLength to less-than-ten when it is clearly and demonstrably not.


You could try using isValid() instead.

<cfif NOT isValid("telephone", phoneNum) >

[THROW ERROR]

</cfif>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜