I want to make (an HTML) 'file' input element mandatory: something like
<input type='file' required = 'required' .../>
But it is not working.
I saw this WW3 manual which states 'required' attribute is new to HTML 5. But I am not using HTML 5 in the project I am working which doesn't support the new feature.
Any idea?
1. Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.
ASP.NET RequiredFieldValidator Control This validator is used to make an input control required.
The required attribute is a boolean attribute. When present, it specifies that the element must be filled out before submitting the form.
<input type="text">
Thanks to HTML5, it is as easy as this:
<input type='file' required />
Example:
<form> <input type='file' required /> <button type="submit"> Submit </button> </form>
You can do it using Jquery like this:-
<script type="text/javascript"> $(document).ready(function() { $('#upload').bind("click",function() { var imgVal = $('#uploadfile').val(); if(imgVal=='') { alert("empty input file"); return false; } }); }); </script> <input type="file" name="image" id="uploadfile" size="30" /> <input type="submit" name="upload" id="upload" class="send_upload" value="upload" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With