form name="form" novalidate autocomplete="off" role="form">
textarea name="firstname" id="firstname" required data-ng-model="name" maxlength="160" minlength="50"></textarea>
div ng-messages="form.firstname.$error" role="alert">
span ng-message="required">
Please enter Name
</span>
span ng-message="minlength">
{{ 50 - name.length}}
</span>
/div>
/form>
So here In the minlength I need to show the length also, but it doesn't show. Please help.
Try this
<textarea name="firstname" id="firstname" required ng-model="firstname" ng-maxlength="160" ng-minlength="50"></textarea>
<div ng-show="myForm.firstname.$dirty" role="alert">
<span ng-show="myForm.firstname.$error.required"> Please enter Name
</span>
<span ng-show="myForm.firstname.$error.minlength"> Please enter atleast 50 characters
</span>
<span ng-show="myForm.firstname.$error.maxlength"> Maximum allowed characters are 160
</span>
</div>
var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<form name="myForm" novalidate autocomplete="off" role="form">
<textarea name="firstname" id="firstname" required ng-model="firstname" ng-maxlength="160" ng-minlength="50"></textarea>
<div ng-show="myForm.firstname.$dirty" role="alert">
<span ng-show="myForm.firstname.$error.required"> Please enter Name
</span>
<span ng-show="myForm.firstname.$error.minlength"> Please enter atleast 50 characters
</span>
<span ng-show="myForm.firstname.$error.maxlength"> Maximum allowed characters are 160
</span>
</div>
</form>
</div>
Here you are trying to do both HTML validation as well as Angular validation
angularjs ng-minlength validation is not working, form still being submitted
If you just want to show min length, You can do this
<span ng-show="firstname.length < 50"> Please enter atleast {{50 - firstname.length}} characters
</span>
var app=angular.module("app",[]);
app.controller("ctrl",function($scope){
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<form name="myForm" novalidate autocomplete="off" role="form">
<textarea name="name" id="firstname" required ng-model="firstname" maxlength="160" minlength="50"></textarea>
<div ng-show="myForm.name.$dirty" role="alert">
<span ng-show="myForm.name.$error.required"> Please enter Name
</span>
<span ng-show="firstname.length < 50"> Please enter atleast {{50 - firstname.length}} characters
</span>
</div>
</form>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With