最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

html5 input email,html

IT圈 admin 40浏览 0评论

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.

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.

发布评论

评论列表 (0)

  1. 暂无评论