function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex Wong 4Alex Wong 4 

Can apex:inputFile have the html5 validation function?

My web page is a form using html5 validation to check blank. All field in my form can use html5 to perform validation, except the inputFile field for uploading attachment. Can apex:inputFile have the html5 validation function? Or is there any way to perform validation for checking whether the attachment field is blank or not? Thanks.
Best Answer chosen by Alex Wong 4
NagendraNagendra (Salesforce Developers) 
Hi Alex,

Please find the solution for the above requirement.

If I'm not mistaken, an apex:inputFile does not support html- pass-through attributes. So you may need to resort to Javascript. You could either validate the value is not blank using Javascript on form submission. Or an alternative would be to set the html-required attribute on page load.

Validate with JS:
<apex:inputFile id='inputFile' value="{!attach.Body}" fileName="{!attach.name}"/>
<apex:commandButton value="Submit" action="{!submit}" onclick='return test();'/>

<script>
     function test(){
         var inputFile = document.querySelectorAll("[id*='inputFile']")[0];
         if(inputFile.value.length === 0)
             return false;

         return true;
     }
</script>

Set required attr:
var files = document.querySelectorAll("input[type='file']");
for(var i = 0; i < files.length; i++){
    files[i].required = true;
}

This works perfectly for me.

Kindly mark it as solved if this helps.

Best Regards,
Nagendra.P

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Alex,

Please find the solution for the above requirement.

If I'm not mistaken, an apex:inputFile does not support html- pass-through attributes. So you may need to resort to Javascript. You could either validate the value is not blank using Javascript on form submission. Or an alternative would be to set the html-required attribute on page load.

Validate with JS:
<apex:inputFile id='inputFile' value="{!attach.Body}" fileName="{!attach.name}"/>
<apex:commandButton value="Submit" action="{!submit}" onclick='return test();'/>

<script>
     function test(){
         var inputFile = document.querySelectorAll("[id*='inputFile']")[0];
         if(inputFile.value.length === 0)
             return false;

         return true;
     }
</script>

Set required attr:
var files = document.querySelectorAll("input[type='file']");
for(var i = 0; i < files.length; i++){
    files[i].required = true;
}

This works perfectly for me.

Kindly mark it as solved if this helps.

Best Regards,
Nagendra.P
This was selected as the best answer
Alex Wong 4Alex Wong 4
Now I am using the following script:
<script>
     function test(){
         var inputFile = document.querySelectorAll("[id*='photo']")[0];
         if(inputFile.value.length === 0)
         {
         alert("Please insert a photo of artist/ group.");
         return false;}
         else{
         return true;}
         }
</script>
This is the picture of what I have achieved:
User-added image\
However, I want to achieve this:
User-added image

Is there any way to achieve this style?