html5 input email,html

html5 input email,html

Unfortunately, all suggestions except from B-Money are invalid for most cases.

Here is a lot of valid emails like:

günter@web.de (German umlaut)

антон@россия.рф (Russian, рф is a valid domain)

chinese and many other languages (see for example International email and linked specs).

Because of complexity to get validation right, I propose a very generic solution:

It checks if email contains at least one character (also number or whatever except another "@" or whitespace) before "@", at least two characters (or whatever except another "@" or whitespace) after "@" and one dot in between. This pattern does not accept addresses like lol@company, sometimes used in internal networks. But this one could be used, if required:

Both patterns accepts also less valid emails, for example emails with vertical tab. But for me it's good enough. Stronger checks like trying to connect to mail-server or ping domain should happen anyway on the server side.

BTW, I just wrote angular directive (not well tested yet) for email validation with novalidate and without based on pattern above to support DRY-principle:

.directive('isEmail', ['$compile', '$q', 't', function($compile, $q, t) {

var EMAIL_PATTERN = '^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$';

var EMAIL_REGEXP = new RegExp(EMAIL_PATTERN, 'i');

return {

require: 'ngModel',

link: function(scope, elem, attrs, ngModel){

function validate(value) {

var valid = angular.isUndefined(value)

|| value.length === 0

|| EMAIL_REGEXP.test(value);

ngModel.$setValidity('email', valid);

return valid ? value : undefined;

}

ngModel.$formatters.unshift(validate);

ngModel.$parsers.unshift(validate);

elem.attr('pattern', EMAIL_PATTERN);

elem.attr('title', 'Invalid email address');

}

};

}])

Usage:

For B-Money's pattern is "@" just enough. But it decline two or more "@" and all spaces.

发布者:admin,转转请注明出处:http://www.yc00.com/news/1692317069a584814.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信