views:

647

answers:

3

In a CF8 form, I'm using a tag such as the following:

<cfinput 
    type = "Text" 
    name = "Num" 
    value = "0" 
    validate = "range,integer" 
    range = "0,1000" 
    validateAt="onBlur,onSubmit" 
    required = "yes" 
    message = "Invalid Value" 
>

When the field loses focus (onBlur), the input is validated for only the first of the conditions in the validate parameter (it changes when I swap the order).

This is the html / JS code that is auto generated:

<input 
    name="Num" 
    id="Num"
    type="text" 
    value="0" 
    onblur="if( !_CF_hasValue(this, 'TEXT', false) 
    && 
    !_CF_checkrange(this.value,0.0,1000.0, true) 
    ||
    !_CF_checkinteger(this.value, true) )
    { _CF_onErrorAlert(new Array('Invalid Value')); }" 
/>

OnSubmit is handled with separate autogenerated JS and works properly.

Am I doing something wrong? Does CF8 not support validating these two conditions together?

A: 

From the generated JS, it definitely looks like it's trying to honor both validations. If it's not working, you may want to debug the JS in Firebug to see what's going on exactly.

JoelFan
+3  A: 

Looking at the JavaScript this created, there is an error in the logic.

Currently, it is:

if (!has_value && !in_range || !is_integer) show_error()

but I think it really should be:

if (!has_value || !in_range || !is_integer) show_error()

Since JavaScript short-circuits logical expressions and && takes precedence over ||, the third check in the original expression never gets executed if the other two return true.

From the looks of it, I would tentatively say that this is a bug. Are you missing any CF patches? Maybe this has been addressed already?

Tomalak
A: 

If the range/integer validation does not work, you could validate with the regular expression

^1?\d{1,3}$

Which means "beginning of string, optional 1, 1-3 digits, end of string".

Not tested, your miles may vary.

Ben Doom
That works for an integer in the range of 0-999, but not, say, 79-1213 (or 0-1000 as in the question). Although I could probably come up with a regex that would work, it probably wouldn't be general purpose.If I roll my own, I'll just do it in JS.
Dennis Williamson
You can also try and fix the JS validation expression as indicated in my answer and just make your own "onblur" event handler.
Tomalak
@dennis Williamson: Actually, it does work for 0-1000, as in the question. It is not a good way to approach 79-1213, but that was not what was specifically asked.
Ben Doom
@Ben Doom: Oops, I misread the regex. It actually matches 0-1999. ^(1000|\d{1,3})$ would work.
Dennis Williamson