最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs input fields empty or not validation - Stack Overflow

programmeradmin2浏览0评论

Plunker

<ul ng-repeat="fields in data">
<li>
  <input type="text" ng-model="fields.field2.i">
</li>

<li>
  <input type="text" ng-model="fields.field2.ii">
</li>

<li>
  <input type="text" ng-model="fields.field2.iii">
</li>
</ul>

How to check all the fields is filled or not in angularjs? Check the ng-model or the json's object?

Plunker

<ul ng-repeat="fields in data">
<li>
  <input type="text" ng-model="fields.field2.i">
</li>

<li>
  <input type="text" ng-model="fields.field2.ii">
</li>

<li>
  <input type="text" ng-model="fields.field2.iii">
</li>
</ul>

How to check all the fields is filled or not in angularjs? Check the ng-model or the json's object?

Share Improve this question edited Sep 25, 2018 at 6:06 Prashant Pokhriyal 3,8374 gold badges31 silver badges41 bronze badges asked Mar 17, 2015 at 13:43 Dean JasonDean Jason 1051 gold badge1 silver badge9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 2

Check this plunker

<!DOCTYPE html>
<html ng-app="plunker">
<head>
   <meta charset="utf-8" />
   <title>AngularJS Plunker</title>
   <script>document.write('<base href="' + document.location + '" />');</script>
   <link rel="stylesheet" href="style.css" />
   <script data-require="[email protected]" src="https://code.angularjs/1.2.28/angular.js" data-semver="1.2.28"></script>
   <script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
   <form name="form">
      <ul ng-repeat="fields in data">
         <li>
            <input type="text" ng-model="fields.field2.i" required>
         </li>
         <li>
            <input type="text" ng-model="fields.field2.ii" required>
         </li>
         <li>
            <input type="text" ng-model="fields.field2.iii" required>
         </li>
      </ul>
      <button type="button" ng-disabled="form.$invalid" ng-click="save()">Save</button>
   </form>
</body>
</html>

Basically, you should use 'required' on all the required fields and only allow the click on the save button if all fields are fulfilled. This is the very basic form of validation, for more information see this link

发布评论

评论列表(0)

  1. 暂无评论