If you want to just mark an input as invalid if typed text is longer than 22, you can use ng-pattern
:
<input type="text" ng-pattern="/^.{0,22}$/" ng-model="formtodotext" ng-model-instant>
Preventing user input if he tries to type 23rd character needs a custom directive:
app.directive('maxLength',function(){
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, el, attrs, ngModelCtrl){
function checkLength(text) {
var old = ngModelCtrl.$modelValue;
if(text.length<=attrs.maxLength) {
return text;
}else{
ngModelCtrl.$setViewValue(old);
ngModelCtrl.$render();
return old;
}
}
ngModelCtrl.$parsers.push(checkLength);
}
}
});
Use it like that:
<input type="text" max-length="22" ng-model="formtodotext" ng-model-instant>