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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Guest UserRank #2,002 · 726 reputationPosted Sep 22, 2016, 5:23 AM EDT
Midhun TpRank #388 · 6.5K reputationPosted Sep 22, 2016, 6:01 AM EDT
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/
Tejas TrivediRank #1,247 · 1.4K reputationPosted Sep 22, 2016, 7:55 AM EDT
Bhushan BhasmeRank #2,348 · 604 reputationPosted Sep 28, 2016, 12:43 AM EDT
MayankRank #4,018 · 371 reputationPosted Nov 21, 2016, 3:55 AM EST
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.
Bikesh SrivastavaRank #108 · 20.3K reputationPosted Nov 21, 2016, 8:45 AM EST