Last active
May 23, 2016 15:42
-
-
Save AdonaiAraya/7b76cb515ef564dc243301dd6d0ac6f6 to your computer and use it in GitHub Desktop.
Digit limit with form submit on enter (on iOS and Android, input type number not submit the form on "enter")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive("digitLimit", function(){ | |
return { | |
require: "^form", | |
restrict: "A", | |
link: function(scope, element, attrs, ctrl){ | |
var max = attrs.digitLimit; | |
element.bind("keypress keydown", function(e){ | |
if(e.type == "keypress"){ | |
var currentValue = element.val(); | |
var isNumber = !isNaN(parseInt(String.fromCharCode(e.keyCode))); | |
if((!isNumber || currentValue.length >= max) && e.keyCode != 8 && e.keyCode != 9){ | |
e.preventDefault(); | |
} | |
} | |
else if(e.type == "keydown"){ | |
if(e.keyCode == 9){ | |
console.log(ctrl); | |
var formElement = angular.element(document.querySelector("form[name='" + ctrl.$name + "']")); | |
var submitFn = formElement.attr("ng-submit"); | |
if(formElement && submitFn){ | |
var formScope = formElement.scope(); | |
formScope.$apply(function(){ | |
formScope.$eval(submitFn); | |
}); | |
} | |
} | |
} | |
}); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment