Can not add js custom validation rule

Add a custom JavaScript validation rule for file uploads in CF.

Overview

The issue occurs due to incorrect JavaScript syntax and improper rule naming in the validation settings.
Ensure the custom rule is named correctly without field prefixes, use jQuery instead of $, and verify there are no console errors when testing the file selection.

Answered
ChronoForms v6
ca cambium 26 Apr, 2019
Trying to add the js custom validation for file type fields , but I didn't succeed.
The file uploading give an error.Can not add js custom validation rule image 1

My js function here:

Can not add js custom validation rule image 2

That's my specified custom rule:
Can not add js custom validation rule image 3


What wrong i'm doing ?
he healyhatman 26 Apr, 2019
Few things.

1) Remove $(document).ready, you've already ticked the "Add inside DOM ready event" button
2) You can't use $() unless you redeclare it, use jQuery() instead.
ca cambium 26 Apr, 2019
Corrected, but error the same ("Form: There is no rule matching the one you specified file1/fileExt")

Can not add js custom validation rule image 4
he healyhatman 27 Apr, 2019
Answer
1 Likes
In the "validation rules" it should just be fileExt['params'] NOT file1/fileExt['params']

And then check for javascript console errors when you click the choose file button
ca cambium 27 Apr, 2019
You're right, of course!
Couldn't think of anything better, but it works for me:
jQuery.fn.form.settings.rules.fileExt = function() { 

if ( jQuery(this).attr('type') == 'file') {
var elem = jQuery(this),
fExt = jQuery(this).val().split('.').pop().toLowerCase(),
array = jQuery.parseJSON(elem.attr('data-validationrules'));

rules_array = array.rules;
rules_sting = JSON.stringify(rules_array).slice(0, -1).slice(1);
rules = jQuery.parseJSON(rules_sting);
param = JSON.stringify(rules.type).split("'");
validExt = param[1].split(",");


if ( jQuery.inArray(fExt, validExt) == -1){
return false;
}else{
return true;
}
}
};// End fileExt
This topic is locked and no more replies can be posted.