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

@mnk

Can this solution be applied to checking the attachment file names for the keywords, i.e. "capacity assessment", "gender marker". Then give you an error message if one of these is missing.

Hello @Jamail_Serio,

Yes, you can also validate the name like this:

fd.spRendered(function(){

fd.field('Attachments').validators.push({
        name: 'Attachments Validation',
        error: "Error message",
        validate: function(value) {
          var validName = true;
          value.forEach(function(file){
            var attachName = file.name;
            if (!attachName.includes('capacity assessment') && !attachName.includes('gender marker')) {
              validName = false
            }
          })
          return validName;
        }
});
});

Hi! I need help again.

The "Capacity Assessment" form and "Gender Marker" form are both required attachments. So I wanted to check the attachment field, that both files have been attached. How do I do this?

Also, thank you for the above code, it is perfect.

Thank you!

@Jamail_Serio,

You can also check how many files is attached like this:

fd.spRendered(function(){
    fd.field('Attachments').validators.push({
            name: 'Attachments Validation',
            error: "Error message",
            validate: function(value) {
              var validName = true;
              if(value.length == 2){
                value.forEach(function(file){
                var attachName = file.name;            
                if (!attachName.includes('capacity assessment') && !attachName.includes('gender marker')) {
                  validName = false
                }

              })
              return validName;
              }
              return false;
            }
    });
});

@mnikitina

This is perfect, thank you.

1 Like