I have an input="text" which is required.
The below code is a directive for text which force user to type only numbers.
By its default required warning is ON. Now the problem is if you type any keyword except numbers in the textbox, it will not type the character ( which is fine ) but the required error will disappear!!!!!! the result is a textbox with empty value and not required error message !!!
angular.module('league').directive('numbersOnly', function () {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function fromUser(text) {
if (text) {
var transformedInput = text.replace(/[^0-9]/g, '');
if (transformedInput !== text) {
ngModelCtrl.$setViewValue(transformedInput);
ngModelCtrl.$render();
}
return transformedInput;
}
return undefined;
}
ngModelCtrl.$parsers.push(fromUser);
}
};
});
<input type="text" class="CreateTxt" ng-model="league.name" name="leagueName" required />
<span ng-show="myForm.leagueName.$error.required" style="color:red">Its required...</span>