【问题标题】:How to restrict specail character Grave accent (backtick) in text box using angular js如何使用角度js限制文本框中的特殊字符重音(反引号)
【发布时间】:2017-09-26 18:55:30
【问题描述】:

我想验证允许像 []^_,atoz,1 到 10 个字符这样的特殊字符的文本框。使用 ng-pattern="/^[a-zA-Z0-9]*$/" 它允许进入,但它没有启用提交按钮。但问题是我们不应该允许用户输入特殊字符,尤其是重音(`):

<input type="text" ng-pattern="/^[a-zA-Z0-9]*$/">

并且还使用以下验证:

<input type="text" restrict="^[^a-zA-Z0-9]*$/">

请帮助我。提前致谢。

【问题讨论】:

    标签: javascript angularjs angularjs-forms angular-validation


    【解决方案1】:

    你可以使用指令

    directive('noSpecialChar', function() {
                return {
                    require: 'ngModel',
                    restrict: 'A',
                    link: function(scope, element, attrs, modelCtrl) {
                        modelCtrl.$parsers.push(function (inputValue) {
    
                                if (inputValue == null)
                                    return ''
                                var cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
                                if (cleanInputValue != inputValue) {
                                    modelCtrl.$setViewValue(cleanInputValue);
                                    modelCtrl.$render();
                                }
                                return cleanInputValue;
    
                        });
                    }
                }
            })
    

    HTML

    <input type='text' no-Special-Char />
    

    【讨论】:

    • 嘿试过这个它允许空格。我需要允许用户输入字母数字,特殊字符,如 []_^。不是空格
    • 您可以从此行中删除所有您想要允许的字符 var cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多