How can I check for both integers and a range in a ColdFusion form?

In CF8 form, I use a tag, for example:

<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 checked only for the first of the conditions in the check parameter (it changes when I change the order).

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

 <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 a separate auto-generated JS and works correctly.

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

+4
source share
3 answers

Looking at the generated JavaScript, 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() 

Because javascript short circuits of logical expressions and && takes precedence over || A third check of the original expression is never performed if the other two return true .

In appearance, I would presumably say that this is a mistake. Do you miss any CF patches? Perhaps this has already been reviewed?

+3
source

From the generated JS, it definitely looks like trying to honor both checks. If it does not work, you can debug JS in Firebug to find out what exactly is happening.

0
source

If range / integer check doesn't work, you can check with regex

 ^1?\d{1,3}$ 

This means "beginning of line, optional 1, 1-3 digits, end of line".

Unverified, your miles may vary.

0
source

All Articles