Expand my Community achievements bar.

SOLVED

File name validation in AEM forms

Avatar

Level 6

Hi Team,

 

I have simple AEM  form in that  there is file  attachment field.

Now the requirement is file name should not contain any special characters if it contains special character i have to show custom error message like "Attached File  name contains special  character"

 if any one did like this please share the knowledge

 

manikanthar1295_0-1649763546962.png

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@manikanthar1295 

That's the purpose of implementing this API i.e to get the file attachment component attributes. In the case at hand, you can set an alert/warning for the user to re-upload the file with the expected name.

Any further implementation (resetting/deleting the value of the component) has to be taken care of using javascript.

View solution in original post

8 Replies

Avatar

Employee Advisor

@manikanthar1295 

OOTB, the file attachment component does not support attaching files with a filename starting with characters (.), containing characters \ / : * ? " < > | ; % $.

Otherwise, you can configure a check with getFileAttachmentsInfo as below:

guideBridge.getFileAttachmentsInfo({
    success: function (list) {
 //some specific file names that are supported     
            } else 
            if (fileName.match(/["add the special character here"]/)) {
              alert("File name cannot contain a special character.");
            }
});

Hope this helps!

 

[0] - 

https://helpx.adobe.com/experience-manager/6-5/forms/javascript-api/GuideBridge.html#getFileAttachme...

 

Avatar

Level 6

@Pulkit_Jain_ ,

guideBridge.getFileAttachmentsInfo({
    success: function (list) {
 //some specific file names that are supported     
            } else 
            if (fileName.match(/["add the special character here"]/)) {
              alert("File name cannot contain a special character.");
            }
});

 

Above code only give the file info like name and path of the file but we are not able to avoid the upload how we can achive the uploading file

 

Avatar

Correct answer by
Employee Advisor

@manikanthar1295 

That's the purpose of implementing this API i.e to get the file attachment component attributes. In the case at hand, you can set an alert/warning for the user to re-upload the file with the expected name.

Any further implementation (resetting/deleting the value of the component) has to be taken care of using javascript.

Avatar

Employee Advisor

@manikanthar1295 This is certainly possible, using 

getFileAttachmentsInfo

You will have to implement logic to delete the uploaded file in case of regex failure.  

Avatar

Level 6

@Mayank_Gandhi  @Pulkit_Jain_ 

 

I am able to get the file info like file name and path of the file based on regex expression i can test the file name with regex now if it contain special character how i can delete that special character file  which object i need to use to delete if you have any example please share here.

 

 

@Mayank_Gandhi   need your input here

Avatar

Employee Advisor

@manikanthar1295 If it's client-side then you should rather prompt the user with a warning and ask him to reupload the file with the correct pattern.

Avatar

Level 6

@Mayank_Gandhi,

guideBridge.getFileAttachmentsInfo will give path and name of file but we are able to upload the file into tmp location how we can make based on regex 

 

if file name fails in regex match we need to avoid uploading the attachment how we can achive this if you have any code please share with us

 

guideBridge.getFileAttachmentsInfo({
    success: function (list) {
 //some specific file names that are supported     
            } else 
            if (fileName.match(/["add the special character here"]/)) {
              alert("File name cannot contain a special character.");
            }
});

 

 

Avatar

Employee Advisor

@manikanthar1295 here is a quick workaround, use the click method with the id (x) of the delete icon that comes with the attachment to remove the file in the failure block.

 

  document.getElementById("myCheck").click();