HTML5 Email Input Pattern Attribute

前端 未结 17 947
时光取名叫无心
时光取名叫无心 2020-11-30 20:09

I’m trying to make a html5 form that contains one email input, one check box input, and one submit input. I\'m trying to use the pattern attribute for the email input but I

相关标签:
17条回答
  • 2020-11-30 20:30

    Updated 2018 Answer

    Go here http://emailregex.com/

    Javascript:

    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

    0 讨论(0)
  • 2020-11-30 20:31

    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:

    <input type="text" pattern="[^@\s]+@[^@\s]+\.[^@\s]+" title="Invalid email address" />
    

    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:

    <input type="text" pattern="[^@\s]+@[^@\s]+" title="Invalid email address" />
    

    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:

    <input type="text" is-email />
    

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

    0 讨论(0)
  • 2020-11-30 20:31
    ^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
    
    0 讨论(0)
  • 2020-11-30 20:34

    This is a dual problem (as many in the world wide web world).

    You need to evaluate if the browser supports html5 (I use Modernizr to do it). In this case if you have a normal form the browser will do the job for you, but if you need ajax/json (as many of everyday case) you need to perform manual verification anyway.

    .. so, my suggestion is to use a regular expression to evaluate anytime before submit. The expression I use is the following:

    var email = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
    

    This one is taken from http://www.regular-expressions.info/ . This is a hard world to understand and master, so I suggest you to read this page carefully.

    0 讨论(0)
  • 2020-11-30 20:34

    If you don,t want to write a whitepaper about Email-Standards, then use my following example which just introduce a well known CSS attribute(text-transform: lowercase) to solve the problem:

    <input type="email" name="email" id="email" pattern="[a-zA-Z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}" style="text-transform: lowercase" placeholder="enter email here ..." title="please enter a valid email" />

    0 讨论(0)
  • 2020-11-30 20:35
    <input name="email" type="email" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" class="form-control" placeholder="Email*" id="email" required="">
    

    This is modified version of above solution which accept capital letter as well.

    0 讨论(0)
提交回复
热议问题