Copy link to clipboard
Copied
I'm working on a customer site running on CF7 on Solaris and am doing a down-and-dirty addition of form field validation and wanted to use cfinput validation. The following attempt to use maxlength:
<cfinput name="FIRST_NAME" type="text" id="First_Name"
size="35" value="#session.stItem.FIRST_NAME#" validate="maxlength"
message="This field can only be 5 characters long."
validateAt="onBlur" maxLength="15" />
causes a JS error box whenever anything is entered into the field. The code it generates on the field is:
onblur="if( ( this.value.length > null) ){ _CF_onErrorAlert(new Array('This field can only be 5 characters long.')); }"
which makes no sense.
If I add in the required attribute, it gets worse -- doing an AND when an OR should have been done. So
<cfinput name="FIRST_NAME" type="text" id="First_Name"
size="35" value="#session.stItem.FIRST_NAME#" validate="maxlength"
required="yes"
message="This field can only be 5 characters long."
validateAt="onBlur" maxLength="15" />
produces the following code:
onblur="if( !_CF_hasValue(this, 'TEXT', false) && ( this.value.length > null) ){ _CF_onErrorAlert(new Array('This field can only be 5 characters long.')); }"
I've googled for info on the above, but didn't find anything. Is there a known bug on this or am I missing something?
Copy link to clipboard
Copied
a maxlength attribute seems so much simpler.
Copy link to clipboard
Copied
David,
If you change the validateAt attribute from onBlur to onSubmit (the default), does it change the resulting JS error?
Copy link to clipboard
Copied
Interestingly, the JS code seems to be right -- it generates:
//form element FIRST_NAME 'MAXLENGTH' validation checks
if( _CF_this['FIRST_NAME'].value.length > 15 )
{
_CF_onError(_CF_this, "FIRST_NAME", _CF_this['FIRST_NAME'].value, "This field can only be 15 characters long.");
_CF_error_exists = true;
}
which looks right. I can probably live with it being onsubmit but it would be nice to have the option to check at entry time.
d
Copy link to clipboard
Copied
Validating at onBlur has a huge downside. The user can still submit the form without changing the "bad" data.