简体   繁体   English

关闭电子邮件验证

[英]Turn off email validation

I have the following piece of html/angular 我有以下的html / angular

<input type="email" data-ng-model="myEmailVar" />
{{ myEmailVar }}

Now the problem is angular has an auto-validator for emails that won't set myEmailVar unless it passes correctly. 现在的问题是,Angular有一个自动验证器,用于电子邮件,除非正确通过,否则不会设置myEmailVar。 So for instance if I enter "name" myEmailVar will not be set. 因此,例如,如果我输入“名称”,则不会设置myEmailVar。 You can see it here: http://jsfiddle.net/bFVsW/ (enter the word test, then enter test@test.test) 您可以在这里看到它: http : //jsfiddle.net/bFVsW/ (输入单词test,然后输入test@test.test)

Now, I want to run my own validation, but also support mobile. 现在,我想运行自己的验证,但也支持移动设备。 If I use type="email" some mobile browsers switch the keyboard layout to make inputting an address easier (such as the @ symbol). 如果我使用type =“ email”,则某些移动浏览器会切换键盘布局以简化地址输入(例如@符号)。 So I can't switch it to type="text". 所以我不能将其切换为type =“ text”。 What i'd like to do is override the angular email validator or just turn it off completely as I'll be handling my own validation. 我想做的是覆盖有角度的电子邮件验证程序,或者完全关闭它,因为我将处理自己的验证。 Is that possible? 那可能吗?

On HTML5 you can use the form's attribute novalidate to disable browser's validation: 在HTML5上,您可以使用表单的属性novalidate禁用浏览器的验证:

<form novalidate>
    <input type="email"/>
</form>

Or you can use type="text" 或者您可以使用type="text"

For any body who is still searching for an answer, I found the answer in this stack overflow answer: How to disable angulars type=email validation? 对于仍在寻找答案的任何机构,我在此堆栈溢出答案中找到了答案: 如何禁用angulars type = email验证?

Essentially you can add your own custom directive that disables the default angular validators. 本质上,您可以添加自己的自定义指令,以禁用默认的角度验证器。

angular.module('app').directive('disableValidators', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl){
            var validator = function(value){
                return value;
            };

            // replace other validators with our own
            ctrl.$parsers = [validator];
            ctrl.$formatters = [validator];
        }
    }
});

Another answer in that same thread presents a more detailed answer: How to disable angulars type=email validation? 同一线程中的另一个答案给出了更详细的答案: 如何禁用angulars type = email验证?

Edit: This solution worked for me in the past, but no longer worked when I upgrade from angular 1.2.X to 1.3.X. 编辑:此解决方案过去对我有用,但是当我从angular 1.2.X升级到1.3.X时不再起作用。 It works, however, with a few minor tweaks: 但是,它可以进行一些小的调整:

angular.module('app').directive('disableValidators', function() {
    return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
            var validator = function(value) {
                return value;
            };

            // replace the email validators
            ctrl.$validators = {email: validator};
        }
    }
});

UPDATE : This does NOT work ... well at least not in a way you'd like it to. 更新:这是行不通的……至少不能以您想要的方式。 Adding ng-non-bindable to the form or any input breaks ALL binding. 将ng-non-bindable添加到表单或任何输入会中断所有绑定。 So, your ng-model in the inputs won't work anymore. 因此,输入中的ng-model将不再起作用。 Sorry .... 对不起...

ng-non-bindable is the answer to this problem. ng-non-bindable是此问题的答案。 See my answer here: 在这里查看我的答案:

https://stackoverflow.com/a/19387233/75644 https://stackoverflow.com/a/19387233/75644

Simple solution: init field with type 'text' and change type to 'email' by timeout. 简单的解决方案:初始化类型为'text'字段,并通过超时将类型更改为'email' In this case angularjs won't add email validator and you will have the email keyboard. 在这种情况下,angularjs将不会添加电子邮件验证程序,而您将拥有电子邮件键盘。

<input type="{{type}}" />

$scope.type = 'text';  
$timeout(function() { 
    $scope.type = 'email'; 
});

http://jsfiddle.net/44pc5j8L/ http://jsfiddle.net/44pc5j8L/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM