Forums

Can not add js custom validation rule

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.

My js function here:



That's my specified custom rule:



What wrong i'm doing ?
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.
cambium 26 Apr, 2019
Corrected, but error the same ("Form: There is no rule matching the one you specified file1/fileExt")

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
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.