1

I want to validate input type=file field using Angular.js. Here is my code:

<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Upload Image :</span>
<input type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage" ngf-select ng-model="file" ngf-pattern="'image/*'" accept="image/*" ngf-max-size="20MB" ngf-min-height="100" ngf-resize="{width: 100, height: 100}" >
</div>

<input type="button" class="btn btn-success" ng-click="addProductInfoData();"  id="addProfileData" ng-value="buttonName"/>

I am using angular.js ngFileUpload module. My requirement is when user will forget to select image and click on Add button one alert message will notify user to select image. How can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • How about `if (form.file.$valid && $scope.file)` ? – Rayon Dec 04 '15 at 11:54
  • I will check and inform you. – satya Dec 04 '15 at 11:55
  • No,Its not checking. – satya Dec 04 '15 at 11:58
  • Have you make basic changes like name of the field and model name ? – Rayon Dec 04 '15 at 12:00
  • What i did,I put console message like `console.log('valid',billdata.bannerimage.$invalid);` but it gave me false when file input is blank. – satya Dec 04 '15 at 12:01
  • http://stackoverflow.com/questions/16207202/required-attribute-not-working-with-file-input-in-angular-js Hope this helps – akgaur Dec 04 '15 at 12:05
  • If you see the first jsfiddle sample of the module in the readme file it does validation of the form: Upload with form submit and validations: http://jsfiddle.net/danialfarid/maqbzv15/38/ You can follow that example. You can just add ng-required or required to the input and check `!myForm.$valid` – danial Dec 04 '15 at 15:03

0 Answers0