【问题标题】:Angular js allow alphabetical caharacters and special characters but not numbersAngularjs 允许字母字符和特殊字符,但不允许数字
【发布时间】:2019-01-24 03:11:34
【问题描述】:

我的指令如下。我想要的是允许字母字符和特殊字符但不允许数字。我们如何根据下面的代码更改它。谢谢。

   app.directive('validEn', function () {
        return {
            require: '?ngModel',
            link: function (scope, element, attrs, ngModelCtrl, SweetAlert) {
                if (!ngModelCtrl) {
                    return;
                }

                ngModelCtrl.$parsers.push(function (val) {
                    var clean = val.replace(/[^a-z|^A-z|^\s]+/g, '');
                    console.log("sfdsfd")
                    if (val !== clean) {
                        ngModelCtrl.$setViewValue(clean);
                        ngModelCtrl.$render();
                    }
                    return clean;
                });

                element.bind('keypress', function (event) {
                    if (event.keyCode === 32) {
                        event.preventDefault();
                    }
                });
            }
        };
    });

【问题讨论】:

  • 嗨@AdamKaylin,如果我的解决方案解决了您的问题,请接受我的回答:) 谢谢

标签: javascript angularjs input angularjs-ng-pattern


【解决方案1】:

您需要更改您的正则表达式语句,以便它用空字符串替换任何数字,例如:

var clean = val.replace(/\d*/g, '');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多