Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making 'file' input element mandatory (required)

Tags:

html

css

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?

like image 703
Bere Avatar asked Jul 15 '13 08:07

Bere


People also ask

How do you make an input compulsory?

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.

Which control is used to make input field compulsory?

ASP.NET RequiredFieldValidator Control This validator is used to make an input control required.

Which attribute is mandatory in form element?

The required attribute is a boolean attribute. When present, it specifies that the element must be filled out before submitting the form.

Is input type text required?

<input type="text">


2 Answers

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>
like image 109
Ibo Avatar answered Sep 19 '22 17:09

Ibo


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" /> 
like image 41
Vivek Sadh Avatar answered Sep 21 '22 17:09

Vivek Sadh