0

I want to show the "required" alert in an input of the type "file", this is hidden (display: none) because it must have a specific style, and this style is applied to its corresponding label. Below is an example:

<label for="add-photo-input" id="add-photo">Adjuntar Foto</label>
<input id="add-photo-input" type="file" required name="add-photo-input" style="display: none;">

3
  • You can always check if the input field has any value or not. Based on that you can show appropriate message. Commented Oct 27, 2017 at 21:11
  • 2
    Don’t use display:none, use opacity:0 Commented Oct 27, 2017 at 21:15
  • @ScottMarcus, thank you very much, I solved the problem with your suggestion Commented Oct 27, 2017 at 21:26

1 Answer 1

1

You could use JavaScript.

<script>
function check() {
    var x = document.getElementById("add-photo-input").value;
    if (x == "") {
        document.getElementById("add-photo").style="border:.1em solid blue";
        alert("Please add photo");
        return false;
    }
} function reset() {
    document.getElementById("add-photo").style="border:none";
}
</script>

And make sure that the check function is called when the submit button is clicked:

<label for="add-photo-input" id="add-photo" onclick="reset()">Adjuntar Foto</label>
<input id="add-photo-input" type="file" required name="add-photo-input" style="display:none">
<input onclick="check()" id="submit" type="submit" />

It would be helpful to have more information, is there a reason you aren't able to change the style of the file input element to match what you want?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.