javascript 带有unicode字符的Angular JS电子邮件验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16863389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:16:30  来源:igfitidea点击:

Angular JS Email Validation with unicode characters

javascriptangularjs

提问by Nanu

I have a sign up form for an application, and angular js is responsible for its validation.

我有一个应用程序的注册表单,angular js 负责对其进行验证。

I ran into an issue when Angular js wasn't accepting an email address which has apostrophe in it. "Pear'[email protected]".

当 Angular js 不接受带有撇号的电子邮件地址时,我遇到了一个问题。“Pear'[email protected]

I found out that angularJs doesnt like unicode characters in email address.

我发现 angularJs 不喜欢电子邮件地址中的 unicode 字符。

Has anyone else came across an issue like this, I am interested in knowing my options to get away with this bug in angularJs.

有没有其他人遇到过这样的问题,我很想知道我在 angularJs 中摆脱这个错误的选择。

Any inputs are appreciated. Thanks !

任何输入表示赞赏。谢谢 !

回答by vittore

If having html5 <input type=email />is not critical, you can use <input type=text />and pattern validation

如果 html5<input type=email />不是关键,您可以使用<input type=text />和模式验证

 <input type="text" ng-model="field" ng-pattern="EMAIL_REGEXP" />

and you can use regex that @Andy Joslin posted in his answer

你可以使用@Andy Joslin 在他的回答中发布的正则表达式

 $scope.EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;

回答by Andrew Joslin

AngularJS uses this regular expression to test email: https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L4

AngularJS 使用这个正则表达式来测试电子邮件:https: //github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L4

What you could do is write a directive that checks it yourself. Just copy the one from AngularJS and use your own regexp: https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L606-L614

您可以做的是编写一个自己检查的指令。只需从 AngularJS 复制一个并使用您自己的正则表达式:https: //github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L606-L614

myApp.directive('nanuEmail', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elm, attrs, model) {
      //change this:
      var EMAIL_REGEXP = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
      var emailValidator = function(value) {
      if (!value || EMAIL_REGEXP.test(value)) {
        model.$setValidity('email', true);
        return value;
      } else {
        model.$setValidity('email', false);
        return undefined;
      }
      model.$parsers.push(emailValidator);
      model.$formatters.push(emailValidator);
    }
  };
});

Then you can just do:

然后你可以这样做:

<input nanu-email ng-model="userEmail">

回答by Nanu

I just updated the regex in the angular.js file (added " '" in the expression) and it worked, without making any other changes.

我刚刚更新了 angular.js 文件中的正则表达式(在表达式中添加了“ '”)并且它起作用了,没有做任何其他更改。

EMAIL_REGEXP = /^[A-Za-z0-9._%+-']+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/ . Thanks Vittore, for giving me the idea to update REGEX. :)

EMAIL_REGEXP = /^[A-Za-z0-9._%+- ']+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/ . 感谢 Vittore,让我想到更新 REGEX。:)

回答by user3476503

why do you return undefined?

你为什么返回未定义?

Refactoring of the function:

函数重构:

var verificationEmail = function (viewValue) {
  if ((typeof viewValue != "undefined") && viewValue != "") {
    return regex.test(viewValue);
  }
};

回答by jitendra Prasad

Angular do not support the apostrophe(') in email Id. If you need to validate the apostrophe in Angular, you need to change the regular expression from:

Angular 不支持'电子邮件 ID 中的撇号( )。如果需要在 Angular 中验证撇号,则需要将正则表达式从:

(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)

To:

到:

/^[A-Za-z0-9._%+'-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/.

It will work perfectly.

它会完美地工作。