how to allow only alphabets and space in textbox using AngularJS
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Bikesh SrivastavaPosted Nov 21, 2016, 1:45 PM
MayankPosted Nov 21, 2016, 8:55 AM
1.var mainApp = angular.module("mainApp", ['ngRoute', 'ngResource']);
mainApp.directive('allowPattern', [allowPatternDirective]);
function allowPatternDirective() {
return {
restrict: "A",
compile: function (tElement, tAttrs) {
return function (scope, element, attrs) {
// I handle key events
element.bind("keypress", function (event) {
var keyCode = event.which || event.keyCode; // I safely get the keyCode pressed from the event.
var keyCodeChar = String.fromCharCode(keyCode); // I determine the char from the keyCode.
// If the keyCode char does not match the allowed Regex Pattern, then don't allow the input into the field.
if (!keyCodeChar.match(new RegExp(attrs.allowPattern, "i"))) {
event.preventDefault();
return false;
}
});
};
}
};
}
2. allow-pattern="^[a-zA-Z ]*$" />
If u think it resolve your issue please marked as answer.
Bhushan BhasmePosted Sep 28, 2016, 4:43 AM
Tejas TrivediPosted Sep 22, 2016, 11:55 AM
Midhun TpPosted Sep 22, 2016, 10:01 AM
Please try like below-
input type="text" pattern="/^[a-zA-Z\s]*$/" ng-pattern-restrict/>
Please have a look at below link too -
https://justforchangesake.wordpress.com/2015/01/13/accept-only-alphabets-for-form-input-field/
Guest UserPosted Sep 22, 2016, 9:23 AM