Check Attachments Extension

I need to check that the attachments are 1 of 3 types of extensions. They can either be PDF, JPG or PNG.

I'm not sure that I am using the correct syntax for this

fd.validators;

fd.validators.push({
    name: 'CheckAttachment',
    error: "Please attach PDF, JPG or PNG documents only",
    validate: function(value) {
        if (fd.field('Attachments').value[0].extension != '.pdf'||fd.field('Attachments').value[0].extension != '.png'||fd.field('Attachments').value[0].extension != '.jpg') {
            return false;
            }
                return true;
            }
});

Hello @SClacherty1,

You can validate the extensions of the attached documents using this code:

fd.spRendered(function(){

fd.field('Attachments').validators.push({
        name: 'Attachments Validation',
        error: "Please attach PDF, JPG or PNG documents only",
        validate: function() {
        for(i = 0; i < fd.field('Attachments').value.length; i++) {
            if (fd.field('Attachments').value[i].extension != '.png' || fd.field('Attachments').value[i].extension !=  '.pdf' || fd.field('Attachments').value[i].extension != ' .jpg') {
                    return false;
                }
            return true;
    }
    }
});
});

Thanks @mnikitina

The code doesn't seem to work. It appears to be the || OR parts - the first option works if I comment out the additional extensions (anything after the first ||)

// Validation of Attachment
   fd.field('Attachments').validators.push({
    name: 'Attachments Validation',
    error: "Please attach PDF, JPG or PNG documents only",
    validate: function() {
    for(i = 0; i < fd.field('Attachments').value.length; i++) {
        if (fd.field('Attachments').value[i].extension != '.png' || fd.field('Attachments').value[i].extension != '.pdf' || fd.field('Attachments').value[i].extension != '.jpg' || fd.field('Attachments').value[i].extension != '.jpeg') {
                return false;
            }
        return true;
}
}   
});

Should this code go before any other code, or should it be the last thing before the final });

I can't work out what the issue with this code is.

@mnikitina

I have managed to get this to work by changing the negative to a positive

// Validation of Attachment
fd.field('Attachments').validators.push({
name: 'AttachmentsValidation',
error: "Please attach PDF, JPG or PNG documents only",
validate: function() {
for(i = 0; i < fd.field('Attachments').value.length; i++) {
if (fd.field('Attachments').value[i].extension === '.png' || fd.field('Attachments').value[i].extension === '.pdf' || fd.field('Attachments').value[i].extension === '.jpg' || fd.field('Attachments').value[i].extension === '.jpeg') {
return true;
}
return false;
}
}
});

Thank you so much for your help

1 Like