【问题标题】:In AngularJS, howto use ng-trim=false with input type as email在 AngularJS 中,如何使用 ng-trim=false 输入类型为电子邮件
【发布时间】:2015-02-25 08:57:09
【问题描述】:

我的登录表单有两个字段..用户名和密码。现在默认情况下,电子邮件字段中的角度修剪空白。 但我想保留空格,以便我可以根据空格显示无效的电子邮件字段消息。 如何在输入类型的电子邮件字段中保留空格? (我知道我们可以使用 ng-trim="false" 来避免修剪,但它是在输入类型 =text only 的情况下应用) 请帮忙。

【问题讨论】:

    标签: angularjs email


    【解决方案1】:

    您的指令如下所示

    指令

    app.directive('customValidation', function() {
        return {
            require: 'ngModel',
            link: function(scope, element, attrs, modelCtrl) {
                modelCtrl.$parsers.push(function(inputValue) {
                    var transformedInput = inputValue.toLowerCase().replace(/ /g, '');
                    if (transformedInput != inputValue) {
                        modelCtrl.$setViewValue(transformedInput);
                        modelCtrl.$render();
                    }
                    return transformedInput;
                });
            }
        };
    });
    

    HTML

    <input ng-model="sth" ng-trim="false" custom-validation>
    

    这里是Reference SO Question

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 2014-08-20
      • 1970-01-01
      • 2011-03-15
      • 2018-09-20
      • 1970-01-01
      • 2023-03-29
      • 2020-03-14
      • 2013-05-04
      相关资源
      最近更新 更多