如何在angular.js中正确执行ng minlength、ng maxlength验证?

dxpyg8gm  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(412)

在这个项目中,, required , max-size-number , min-size-number 零件工作,但是 pattern , minlength ,及 maxlength 零件不工作,
我怎样才能纠正这个问题?
这里是相关的代码部分;

  1. <div ng-switch-when="TextBox">
  2. <label class="item item-input item-stacked-label">
  3. <span ng-if="haveHeader" class="input-label">{{header}}</span>
  4. <input type="text" style="width:100%"
  5. event="component.events"
  6. validation="component"
  7. ng-blur="isInAdminFormPage && checkTextTypeNumberValidation(component.shortName)"
  8. name="name{{component.shortName}}"
  9. metadata="component.options" ng-disabled="isFormViewed" string-to-date="component.options" ng-required="requiredValidation[component.shortName]"
  10. ng-model="formData[component.shortName]" string-to-number="component.options" >
  11. </label>
  12. <br>
  13. <div ng-if="componentValidationType[component.shortName] == validationType.VALIDATION">
  14. <div style="color: red" ng-messages="getFormValidations(form.shortName)['name'+component.shortName].$error" ng-show="isInAdminFormPage && formInf.mainForm.$submitted">
  15. <div ng-message="required">{{message["required"]}}</div>
  16. <div ng-message="minlength">{{message["minlength"]}}</div>
  17. <div ng-message="maxlength">{{message["maxlength"]}}</div>
  18. <div ng-message="ng-pattern">{{message["ng-pattern"]}}</div>
  19. </div>
  20. <div ng-if="isInAdminFormPage" style="color: red" ng-show="showValidationMessageOfTextBox(component.shortName) && formInf.mainForm.$submitted">
  21. <div ng-show="!maxNumberValidation[component.shortName].validation">{{message["max-number-size"]}}</div>
  22. <div ng-show="!minNumberValidation[component.shortName].validation">{{message["min-number-size"]}}</div>
  23. </div>
  24. </div>

这些验证在何处使用的图片:

yfwxisqw

yfwxisqw1#

很难复制您的示例,但我认为这将为您提供集成所需的所有信息 ng-pattern , ng-minlengthng-maxlength ,以及如何侦听其验证器。

  1. 'use strict';
  2. var app = angular.module('app', []);
  3. app.controller('MainCtrl', ['$scope', function($scope) {
  4. $scope.theInput = '';
  5. $scope.minlength = 5;
  6. $scope.maxlength = 10;
  7. $scope.thePattern = /^[a-zA-Z\s]*$/; ///^[a-zA-Z]\w{3,14}$/;
  8. $scope.theNumberInput = '';
  9. $scope.theNumberPattern = /^((?!5)[0-9]){1,3}$/; ///^[a-zA-Z]\w{3,14}$/;
  10. }]);
  1. .error {
  2. margin: 10px 0;
  3. padding: 5px;
  4. background-color: #eee;
  5. color: #f00;
  6. border: 1px solid #ccc;
  7. }
  8. input.ng-dirty.ng-invalid {
  9. background-color: #f65b90;
  10. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
  2. <div id="mainContainer" ng-app="app" ng-controller="MainCtrl">
  3. <ng-form role="form" name="theForm" novalidate>
  4. <p>
  5. Enter a word between 5 and 10 characters long - no numbers, no periods
  6. </p>
  7. <input type="text" ng-required="true" name="theInput" ng-model="theInput" ng-maxlength="{{maxlength}}" ng-minlength="{{minlength}}" ng-pattern='thePattern' />
  8. <div class='feedback' ng-if='theForm.$dirty'>
  9. <div ng-show="!theForm.theInput.$valid" class="error">
  10. Not valid
  11. </div>
  12. <div ng-show="theForm.theInput.$error.pattern" class="error">
  13. Input pattern is not valid.
  14. </div>
  15. <div ng-show="theForm.theInput.$error.minlength" class="error">
  16. Input length is less than the min length ({{minlength}}) </div>
  17. <div ng-show="theForm.theInput.$error.maxlength" class="error">
  18. Input length is greater than the max length ({{maxlength}})
  19. </div>
  20. </div>
  21. <p>
  22. Enter a number between 1 and 999 with no 5's in it
  23. </p>
  24. <input type="number" ng-required="true" name="theNumberInput" ng-model="theNumberInput" ng-pattern='theNumberPattern' />
  25. <div class='feedback'>
  26. <div ng-show="theForm.theNumberInput.$error.pattern" class="error">
  27. Input pattern is not valid.
  28. </div>
  29. </div>
  30. </ng-form>
  31. </div>
展开查看全部

相关问题