1

When i define a ng-minlength and ng-pattern, this code doesn't work good :

<span class="myForm_textarea" ng-show="myFormZR.textarea.$dirty"><br /><br />characters : {{0 + myForm.textarea.length}} writed / 500 limit</span>

count is go to 0 when a string matche the regex or count is 0 until the minlength is reached. i made a plunkr for that : http://plnkr.co/edit/R6kGJmlQ4TgGf16kAmYi?p=preview

5
  • It's by design that the binding model will be set to undefined if it's invalid. To workaround that, I need to know first which version of angularjs you are using? Commented Aug 4, 2014 at 4:18
  • 1
    Unfortunately, AFAIK, there is no known workaround for the angular 1.3 yet. But it seems this feature is planned in 1.3 milestone, see #8290. Your only option seems to be downgrade to use angular 1.2.x. Commented Aug 4, 2014 at 5:06
  • same issue with 1.2.21 ... Commented Aug 4, 2014 at 11:26
  • It doesn't work out of the box, but there is a workaround for 1.2.x. If you are going to use 1.2.21, I could provide you a workaround. Commented Aug 4, 2014 at 11:28
  • ok, i'm interesting in that, can you put in in the plunkr ? Commented Aug 4, 2014 at 11:31

3 Answers 3

4

Here's a little workaround that I started using for this specific problem within forms.

Instead of using myForm.textarea.length try using myForm.textarea.$viewValue.length. This way, validation is tracking your model and you still have an accurate way to keep track of your character count.

Sign up to request clarification or add additional context in comments.

Comments

2

In angularjs 1.2.x, you could workaround the problem by writing a custom directive to restore the invalid value.

app.directive('allowInvalid', function () {
  return {
    restrict: 'A',
    require: 'ngModel',
    priority: 1, // force the postLink below to run after other directives
    link: function (scope, element, attrs, modelCtrl) {
      modelCtrl.$parsers.push(function (viewValue) {
        return (viewValue === undefined) ? modelCtrl.$viewValue : viewValue;
      });
    }
  }
});

and put it in the textarea:

<textarea ng-model="myForm.textarea" ng-required="true" ng-minlength="5" ng-maxlength="500"  ng-pattern="textareaRegex" allow-invalid></textarea>

Example plunker: http://plnkr.co/edit/H0YePKFwkvaBZgnxCRMB?p=preview

Comments

-1

If you want to limit the maximum characters in the textarea, you can use maxlength attribute (in addition to the ng-maxlength):

<textarea ng-maxlength="500"  maxlength="500" />

3 Comments

It's not the problem, i already now that. Problem is the bad length count with ng-minlength and ng-pattern.
actually that's the way angular validation works with ng-maxlength. If you want different behaviour, you can write your own maxlength directive
I do not understand how it can be a problem related to maxlength because when i don't use it, my problem is still present. Problem still present too with ng-pattern when minlength is not used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.