1

I need to do validation in autocomplete like

  • min length
  • max length
  • total number of chip

I dint find a correct way to validate all these validations my HTML is

<md-chips md-on-add="selectedGroups.add($chip)" ng-focus="userTag"
                    ng-model="selectedGroups" is="tagSelect" md-autocomplete-snap
                    md-require-match="true" md-max-chips="3" maxlength="10">
                <md-autocomplete 
                    md-search-text="searchText"
                    md-items="item in queryGroups(searchText)" 
                    md-item-text="item"
                    md-autoselect="true" md-select-on-match="true"
                    md-no-cache="true"
                    md-require-match="true"
                    md-input-name="autocompleteField"
                    md-input-minlength="2"
                    md-input-maxlength="5"
                    >
                     <span
                    md-highlight-text="searchText">{{item}}</span>
                    <div ng-messages="searchForm.autocompleteField.$error" ng-if="searchForm.autocompleteField.$touched">
                <div  ng-message="required">You <b>must</b> have a favorite state.</div>
                <div  ng-message="minlength">Your entry is not long enough.</div>
                <div  ng-message="maxlength">Your entry is too long.</div>
              </div>
                     </md-autocomplete> 
                    <md-chip-template
                    ng-maxlength="2" md-max-chips="5" required> <span>{{$chip}}</span>
                </md-chip-template> </md-chips>

my js is

   var allGroups = ['one','two','three'  ];
$scope.queryGroups = function(search) {

    var firstPass = $filter('filter')($scope.allGroups, search);
       return firstPass.filter(function(item) {
        return $scope.selectedGroups.indexOf(item) === -1;
        console.log(item)
    });

};

please help me

Paul Cheriyan
  • 638
  • 9
  • 19

1 Answers1

1
  • total number of chip

    For the total number of chips, use md-max-chips (it's your max number +1 let's say i want 5 chips then i use md-max-chips="6") in your md-chips directive it will disable the input if max is reached. If you want some error output you can create a span below your chips directive with ng-if on the max length of your model (ng-if="model.length >= 6)

  • Min / Max length of the input

    This is a little trick since you want to use an autocomplete the md-maxlength doesn't exist. So what you can do is just substring the string on text change so that it can never have more than 5 characters (md-search-text-change="text_model = text_model.substring(0,5)")

    <md-chips
     name="label"
     ng-model="selectedGroups"
     md-max-chips="3"
     md-removable="true"
     md-on-add="someCheckOnAdd($chip)"
     md-on-remove="someCheckOnDelete($chip)">
       <md-autocomplete 
         md-search-text="text_model"
         ng-disabled="selectedGroups.length >= 3"
         md-items="item in queryGroups(text_model)"
         md-item-text="item"
         md-autoselect="true"
         md-search-text-change="text_model = text_model.substring(0,5)"
         md-select-on-match="true"
         md-no-cache="false"
         md-require-match="true">
           <span md-highlight-text="searchText">{{item}}</span>
       </md-autocomplete>
            <md-chip-template >
                <strong style="padding-right: 10px;">{{$chip}}</strong>
            </md-chip-template>
    </md-chips>
    
    <span ng-if="model.length > 7" class="alert-message-edition-match">Max chips number reached</span>
    

Here is a working fiddle which fits what you wanted to do.