【问题标题】:Angular validate input type="email" as type="text"角度验证输入类型=“电子邮件”为类型=“文本”
【发布时间】:2023-03-29 16:34:01
【问题描述】:

我的 Angular 应用有一个带有电子邮件/用户名输入字段的登录表单:

<input type="email" required/>

输入是 type="email" 所以correct keyboard is displayed in iOS
但是验证应该是 type="text" 输入以允许用户名。

基本上我需要覆盖 Angular 以 将 type="email" 验证为 type="text"
这是一个basic Plunker 开始使用。

注意:我尝试使用 ng-pattern 验证正则表达式,但它似乎只适用于 type="text"

【问题讨论】:

    标签: javascript validation angularjs


    【解决方案1】:

    我认为不可能在不更改其来源的情况下更改 input type='email' 的角度处理方式,但也许您可以尝试:

    <input type="text" required late-email />
    
    angular.module('yourApp').directive('lateEmail', function () {
        return {
            priority: -1,
            link: function(scope, elem, attrs) {
                elem.attr('type', 'email');
            }
        };
    });
    

    这里重要的是优先级,因为angularjs输入指令需要首先运行(链接)所以将使用'text'作为输入类型,然后lateEmail会将输入类型更改为电子邮件。

    【讨论】:

    • 用 Codefather 的解决方案分叉了 Plunk。我现在手头没有 iPhone,但看着检查员我认为它会起作用 - plnkr.co/edit/GAlhFQlMhGNHeSs2TKrJ?p=preview
    • 在 iPad 上测试过,效果不错。感谢 Codefather,标记为正确。
    猜你喜欢
    • 2017-01-02
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    相关资源
    最近更新 更多