HTML5 电子邮件输入模式属性

发布于 2024-10-31 00:50:53 字数 418 浏览 6 评论 0 原文

我正在尝试制作一个 html5 表单,其中包含一个电子邮件输入、一个复选框输入和一个提交输入。 我正在尝试使用电子邮件输入的模式属性,但我不知道要在此属性中放置什么。我确实知道我应该使用必须与 JavaScript 模式产生式匹配的正则表达式,但我不知道该怎么做。

我试图让这个属性做的是检查以确保电子邮件包含一个@和至少一个或多个点,如果可能的话检查@后面的地址是否是真实地址。如果我无法通过此属性执行此操作,那么我会考虑使用 JavaScript,但为了检查一个 @ 和一个或多个点,我确实想使用模式属性。

模式属性需要检查:

  1. 只有一个@
  2. 一个或多个点
  3. 如果可能的话,检查@后面的地址是否是有效地址

另一种方法是使用JavaScript,但对于所有其他条件,我不这样做想要使用 JavaScript。

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 don't know what to place in this attribute. I do know that I'm supposed to use a regular expression that must match the JavaScript Pattern production but I don't know how to do this.

What I'm trying to get this attribute to do is to check to make sure that the email contains one @ and at least one or more dot and if possible check to see if the address after the @ is a real address. If I can't do this through this attribute then I'll consider using JavaScript but for checking for one @ and one or more dot I do want to use the pattern attribute for sure.

The pattern attribute needs to check for:

  1. Only one @
  2. One or more dot
  3. And if possible check to see if the address after the @ is a valid address

An alternative to this one is to use a JavaScript but for all the other conditions I do not want to use a JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

懒猫 2024-11-07 00:50:53

我在 HTML5 电子邮件输入中遇到了这个问题,使用上面 Alwin Keslers 的答案,我将正则表达式添加到 HTML5 电子邮件输入中,因此用户必须在末尾添加 .something 。

<input type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" />

I had this exact problem with HTML5s email input, using Alwin Keslers answer above I added the regex to the HTML5 email input so the user must have .something at the end.

<input type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" />
ゝ偶尔ゞ 2024-11-07 00:50:53

不幸的是,除了 B-Money 之外的所有建议在大多数情况下都是无效的。

这里有很多有效的电子邮件,例如:

  • [email protected](德语元音变音)
  • антон@россия.рф(俄语,рф 是有效域名)
  • 中文和许多其他语言(参见示例 国际电子邮件和链接规格)。

由于正确验证的复杂性,我提出了一种非常通用的解决方案:

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

它检查电子邮件在“@”之前是否至少包含一个字符(也可以是数字或除另一个“@”或空格之外的任何字符),至少两个字符(或除“@”之后是另一个“@”或空格),中间有一个点。此模式不接受像 lol@company 这样的地址,有时在内部网络中使用。但如果需要,可以使用此模式:

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

两种模式也接受不太有效的电子邮件,例如带有垂直选项卡的电子邮件。但对我来说这已经足够好了。无论如何,应该在服务器端进行更严格的检查,例如尝试连接到邮件服务器或 ping 域。

顺便说一句,我刚刚编写了 Angular 指令(尚未经过充分测试),用于使用 novalidate 进行电子邮件验证,并且没有基于上面的模式来支持 DRY 原则:

.directive('isEmail', ['$compile', '$q', 't', function($compile, $q, t) {
    var EMAIL_PATTERN = '^[^@\\s]+@[^@\\s]+\\.[^@\\s]+

用法:

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

对于 B-Money 的模式是“@”就足够了。但它拒绝两个或多个“@”和所有空格。

; 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'); } }; }])

用法:


对于 B-Money 的模式是“@”就足够了。但它拒绝两个或多个“@”和所有空格。

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

Here is a lot of valid emails like:

  • [email protected] (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]+

Usage:

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

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

; 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.

小矜持 2024-11-07 00:50:53

这是一个双重问题(就像万维网世界中的许多问题一样)。

你需要评估浏览器是否支持html5(我使用Modernizr来做到这一点)。在这种情况下,如果您有一个正常的表单,浏览器将为您完成这项工作,但如果您需要 ajax/json (就像许多日常情况一样),您无论如何都需要执行手动验证。

..所以,我的建议是在提交之前随时使用正则表达式进行评估。我使用的表达式如下:

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

这个取自 http://www.regular-expressions.info/ 。这是一个很难理解和掌握的世界,所以我建议您仔细阅读本页。

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.

別甾虛僞 2024-11-07 00:50:53

在 HTML5 中,您可以使用新的“电子邮件”类型: http://www .w3.org/TR/html-markup/input.email.html

例如:

<input type="email" id="email" />

如果浏览器实现 HTML5,它将确保用户在字段中输入了有效的电子邮件地址。请注意,如果浏览器未实现 HTML5,它将被视为“文本”类型,即:

<input type="text" id="email" />

In HTML5 you can use the new 'email' type: http://www.w3.org/TR/html-markup/input.email.html

For example:

<input type="email" id="email" />

If the browser implements HTML5 it will make sure that the user has entered a valid email address in the field. Note that if the browser doesn't implement HTML5, it will be treated like a 'text' type, ie:

<input type="text" id="email" />
时光病人 2024-11-07 00:50:53

这是我正在使用的方法,您可以根据需要进行修改:

^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|[.][\w-]{2,}[.][a-zA-Z]{2,})$

说明:

  1. 我们希望确保电子邮件地址始终以单词开头:

    ^[\w]

一个单词是任何字符、数字或下划线。您可以使用 [a-zA-Z0-9_] 模式,但它会给您相同的结果,而且更长。

  1. 接下来,我们要确保至少有一个这样的字符:

    ^[\w]{1,}

  2. 接下来,我们希望名称中可以包含任何单词、数字或特殊字符。这样,我们就可以确保电子邮件不会以点开头,但可以在第一个位置以外的位置包含点:

    ^[\w]{1,}[\w.+-]

  3. 当然,不必有任何此类字符,因为电子邮件地址只能包含一个字母,后跟@:

    ^[\w]{1,}[\w.+-]{0,}

  4. 接下来,我们需要 @ 字符这是必填项,但整封邮件中只能有一个:

    ^[\w]{1,}[\w.+-]{0,}@

  5. 在 @ 字符后面,我们需要域名姓名。在这里,您可以定义最少需要多少个字符以及字符范围。我会选择所有单词字符,包括连字符 [\w-],并且我想要至少其中两个 {2,}。如果您想允许 t.co 等域名,则必须允许 {1,} 范围内的一个字符:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}

  6. 接下来,我们需要处理两种情况。要么只是域名后跟域名扩展,要么是子域名后跟域名后跟扩展名,例如 abc.comabc.co.uk。为了实现这一点,我们需要使用 (a|b) 标记,其中 a 代表第一种情况,b 代表第二种情况case 和 | 代表逻辑或。在第一种情况下,我们将仅处理域扩展名,但由于无论情况如何它都始终存在,因此我们可以安全地将其添加到这两种情况中:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2, }|[.][a-zA-Z]{2,})

这种模式表明我们只需要一个点字符,后跟字母,没有数字,并且在这两种情况下我们都需要至少两个点字符。

  1. 对于第二种情况,我们将域名添加在域名后缀前面,从而使原始域名成为子域名:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|< strong>[.][\w-]{2,}[.][a-zA-Z]{2,})

域名可以由以下组成单词字符包括连字符,同样,我们在这里至少需要两个字符。

  1. 最后,我们需要标记整个模式的结束:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|[ .][\w-]{2,}[.][a-zA-Z]{2,})$

  2. 转到此处测试您的电子邮件是否与以下模式匹配:https://regex101.com/r/374XLJ/1

This is the approach I'm using and you can modify it based on your needs:

^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|[.][\w-]{2,}[.][a-zA-Z]{2,})$

Explanation:

  1. We want to make sure that the e-mail address always starts with a word:

    ^[\w]

A word is any character, digit or underscore. You can use [a-zA-Z0-9_] pattern, but it will give you the same result and it's longer.

  1. Next, we want to make sure that there is at least one such character:

    ^[\w]{1,}

  2. Next, we want to allow any word, digit or special characters in the name. This way, we can be sure that the e-mail won't start with the dot, but can contain the dot on other than the first position:

    ^[\w]{1,}[\w.+-]

  3. And of course, there doesn't have to be any of such character because e-mail address can have only one letter followed by @:

    ^[\w]{1,}[\w.+-]{0,}

  4. Next, we need the @ character which is mandatory, but there can be only one in the whole e-mail:

    ^[\w]{1,}[\w.+-]{0,}@

  5. Right behind the @ character, we want the domain name. Here, you can define how many characters you want as minimum and from which range of characters. I'd go for all word characters including the hyphen [\w-] and I want at least two of them {2,}. If you want to allow domains like t.co, you would have to allow one character from this range {1,}:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}

  6. Next, we need to deal with two cases. Either there's just the domain name followed by the domain extension, or there's subdomain name followed by the domain name followed by the extension, for example, abc.com versus abc.co.uk. To make this work, we need to use the (a|b) token where a stands for the first case, b stands for the second case and | stands for logical OR. In the first case, we will deal with just the domain extension, but since it will be always there no matter the case, we can safely add it to both cases:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|[.][a-zA-Z]{2,})

This pattern says that we need exactly one dot character followed by letters, no digits, and we want at least two of them, in both cases.

  1. For the second case, we will add the domain name in front of the domain extension, thus making the original domain name a subdomain:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|[.][\w-]{2,}[.][a-zA-Z]{2,})

The domain name can consist of word characters including the hyphen and again, we want at least two characters here.

  1. Finally, we need to mark the end of the whole pattern:

    ^[\w]{1,}[\w.+-]{0,}@[\w-]{2,}([.][a-zA-Z]{2,}|[.][\w-]{2,}[.][a-zA-Z]{2,})$

  2. Go here and test if your e-mail matches the pattern: https://regex101.com/r/374XLJ/1

萌化 2024-11-07 00:50:53

一个简单的好答案可以是这样的输入:

2021 UPDATED & 2021 UPDATED &支持IE10+

^(?![_.-])((?![_.-][_.-])[a-zA-Z\d_.-]){0,63 }[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\ .){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}$

input:not(:placeholder-shown):invalid{
  background-color:pink;
  box-shadow:0 0 0 2px red;
}
/* :not(:placeholder-shown) = when it is empty, do not take as invalid */
/* :not(:-ms-placeholder-shown) use for IE11 */
/* :invalid = it is not followed pattern or maxlength and also if required and not filled */
/* Note: When autocomplete is on, it is possible the browser force CSS to change the input background and font color, so i used box-shadow for second option*/
Type your Email:
<input 
  type="email"
  name="email"
  lang="en"
  maxlength="254"
  value=""
  placeholder="[email protected]"
  autocapitalize="off" spellcheck="false" autocorrect="off"
  autocomplete="on"
  required=""
  inputmode="email"
  pattern="(?![_.-])((?![_.-][_.-])[a-zA-Z\d_.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}">

根据以下内容:

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode
https://developer.mozilla.org/en-US /docs/Web/HTML/Element/input/email

  • URL 中域的最大长度(254 个字符)
  • 可能的最长子域(可能 =0-64 个字符)、域名(=0-64 个字符)、一级扩展名(=2-14 个字符)、二级扩展名(可能 =2 -14 个字符)正如 @Brad 所暗示的。
  • 避免在电子邮件名称中使用不常见但允许的字符,而只接受著名的免费电子邮件服务(如 Outlook、Yahoo、Gmail 等)只会接受的常见字符。这意味着仅接受: 点 (.)破折号 (-)下划线 (_) 位于 az 之间>(小写)或AZ大写 - 只是因为它很常见 - 感谢 Badri Paudel)和数字,并且不接受彼此相邻的两个,最多64字符。

注意:目前,URL 中可以包含更长的地址,甚至 Unicode 字符,用户也可以向本地或 IP 发送电子邮件,但我认为如果目标不接受异常的内容,最好还是不要接受页面是公开的。

正则表达式的解释:

  1. (?![_.-]) 不是以这些开头: _ 。 -
  2. ((?!--)[a-zA-Z\d-]) 接受 a 到 z 和 A 到 Z 以及数字和 - (破折号),但不接受 --
  3. (( ?![_.-][_.-])[a-zA-Z\d_.-]) 从 a 到 z 小写,A 到 Z 大写,还有数字和 _ 。 - 接受但不接受任何类型的双重。
  4. {0,63} 长度从 0 到 63(第二组 [a-zA-Z\d] 将填充 +1,但不要让字符成为_ . -)
  5. @ at 符号
  6. (rule){1,2} 此规则应该存在 1 或 2 次。 (对于子域和域)
  7. (rule)?((rule)|) 不存在,或者如果存在应遵循规则。 (对于二级扩展)
  8. \.

注意:为了对大写更加严格,您可以删除所有AZ从模式。

注意:对于波斯/阿拉伯数字不严格 ٠١٢٣٤٥٦٧٨٩ 012334567789 你可以添加 \u0660- \u0669\u06f0-\u06f9 位于模式中所有 \d 旁边。

尝试正则表达式:https://regexr.com/64kjf

注意:使用^...$ 在输入模式中不是必需的,但对于一般的正则表达式测试将需要。这意味着字符串的开始/结束应该与规则相同,而不仅仅是一部分。

属性解释:

type="email"type="text"email 在现代浏览器中将有助于验证,但它不会不关心开头或结尾的空格以进行验证或获取值)

name="email" autocomplete="on" 为了让浏览器记住简单的最后填写的输入以自动完成

lang="en"< /code> 帮助默认输入为英文

inputmode="email" 将有助于触摸键盘更加兼容

maxlength="254" 设置输入的最大长度

autocapitalize="off"pellcheck="false"auto Correct="off" 关闭浏览器中可能出现错误的自动更正器

required="" 如果该字段为空或无效,则此字段为必填项, 表单未提交

pattern="..." 里面的正则表达式会检查验证


\w=a-zA-Z\d_ so:

最轻版本
(?![_.-])((?![_.-][_.-])[\w.-]){0,63}[a-zA-Z\d]@( (?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA -Z]{2,14}\.)?[a-zA-Z]{2,14}

A simple good answer can be an input like this:

2021 UPDATED & Support IE10+

^(?![_.-])((?![_.-][_.-])[a-zA-Z\d_.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}$

input:not(:placeholder-shown):invalid{
  background-color:pink;
  box-shadow:0 0 0 2px red;
}
/* :not(:placeholder-shown) = when it is empty, do not take as invalid */
/* :not(:-ms-placeholder-shown) use for IE11 */
/* :invalid = it is not followed pattern or maxlength and also if required and not filled */
/* Note: When autocomplete is on, it is possible the browser force CSS to change the input background and font color, so i used box-shadow for second option*/
Type your Email:
<input 
  type="email"
  name="email"
  lang="en"
  maxlength="254"
  value=""
  placeholder="[email protected]"
  autocapitalize="off" spellcheck="false" autocorrect="off"
  autocomplete="on"
  required=""
  inputmode="email"
  pattern="(?![_.-])((?![_.-][_.-])[a-zA-Z\d_.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}">

According to the following:

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email

  • Maximum length of domain in URL (254 Character)
  • Longest possible Subdomain(Maybe =0-64 character), Domain (=0-64 character), First Level Extension(=2-14 character), Second Level Extension(Maybe =2-14 character) as @Brad motioned.
  • Avoiding of not usual but allowed characters in Email name and just accepting usual characters that famous free email services like Outlook, Yahoo, Gmail etc. will just accept them. It means accepting just : dot (.), dash (-), underscore (_) just in between a-z (lowercase) or A-Z (uppercase - just because it is common - thanks to Badri Paudel) and numbers and also not accepting double of them next to each other and maximum 64 characters.

Note: Right now, longer address and even Unicode characters are possible in URL and also a user can send email to local or an IP, but i think still it is better to not accepting unusual things if the target page is public.

Explain of the regex:

  1. (?![_.-]) not started with these: _ . -
  2. ((?!--)[a-zA-Z\d-]) accept a till z and A till Z and numbers and - (dash) but not --
  3. ((?![_.-][_.-])[a-zA-Z\d_.-]) from a till z lowercase and A till Z uppercase and numbers and also _ . - accepted but not any kind of double of them.
  4. {0,63} Length from zero till 63 (the second group [a-zA-Z\d] will fill the +1 but just do not let the character be _ . -)
  5. @ The at sign
  6. (rule){1,2} this rule should exist 1 or 2 times. (for Subdomain & Domain)
  7. (rule)? or ((rule)|) not exist or if exist should follow the rule. (for Second Level Extension)
  8. \. Dot

Note: For being more strict about uppercase you can remove all A-Z from the pattern.

Note: For being not strict about Persian/Arabic numbers ٠١٢٣٤٥٦٧٨٩ ۰۱۲۳۴۵۶۷۸۹ you can add \u0660-\u0669\u06f0-\u06f9 next to all \d in the pattern.

Try the RegEx: https://regexr.com/64kjf

Note: Using ^...$ is not necessary in input pattern, but for general RegEx testing will be needed. It means start / end of the string should be same as the rule, not just a part.

Explaining of attributes:

type="email" or type="text" (email In modern browsers will help for the validation also it don't care spaces in start or end for the validation or getting value)

name="email" autocomplete="on" To browser remember easy last filled input for auto completing

lang="en" Helping for default input be English

inputmode="email" Will help to touch keyboards be more compatible

maxlength="254" Setting the maximum length of the input

autocapitalize="off" spellcheck="false" autocorrect="off" Turning off possible wrong auto correctors in browser

required="" This field is required to if it was empty or invalid, form be not submitted

pattern="..." The regex inside will check the validation


\w=a-zA-Z\d_ so:

Lightest version
(?![_.-])((?![_.-][_.-])[\w.-]){0,63}[a-zA-Z\d]@((?!-)((?!--)[a-zA-Z\d-]){0,63}[a-zA-Z\d]\.){1,2}([a-zA-Z]{2,14}\.)?[a-zA-Z]{2,14}

高冷爸爸 2024-11-07 00:50:53
<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="">

这是上述解决方案的修改版本,它也接受大写字母。

<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.

暖风昔人 2024-11-07 00:50:53

如果您不想编写有关电子邮件标准的白皮书,请使用我的以下示例,该示例仅介绍一个众所周知的 CSS 属性(text-transform:小写)来解决问题:

如果您确实希望数据不以小写值形式到达服务器端,那么您应该这样做:

<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 ..." required />


如果您确实希望数据以小写值形式到达服务器端,那么您应该这样做:

  const emailElmtRegex = new RegExp('[a-zA-Z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}');
document.getElementById("entered").innerHTML = "";
document.getElementById("send").innerHTML = ""

function lower() {
    let emailElmt = document.getElementById("email");
    document.getElementById("entered").innerHTML = "Entered: " + emailElmt.value;
    emailElmt.value = emailElmt.value.toLowerCase();

    if (emailElmtRegex.test(emailElmt.value)) {
        document.getElementById("send").innerHTML = "Send: " + emailElmt.value;
    } else {
        document.getElementById("send").innerHTML = ""
    }
}
input[type=email]#email {
   "text-transform: lowercase
}
<!DOCTYPE html>
<html>
<body>
<h3>Client Side to Server Side - Simple Email validation!</h3>
<input type="email" name="email" id="email" pattern="[a-zA-Z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}" placeholder="enter email here ..." required oninput="lower()" />

<p id="entered">Entered:</p>
<p id="send">Send:</p>

</body>
</html>

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:

If you do want the data not to reach the server side as lower case value, then you should go this way:

<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 ..." required />


If you do want the data to reach the server side as lower case value, then you should go this way:

  const emailElmtRegex = new RegExp('[a-zA-Z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}');
document.getElementById("entered").innerHTML = "";
document.getElementById("send").innerHTML = ""

function lower() {
    let emailElmt = document.getElementById("email");
    document.getElementById("entered").innerHTML = "Entered: " + emailElmt.value;
    emailElmt.value = emailElmt.value.toLowerCase();

    if (emailElmtRegex.test(emailElmt.value)) {
        document.getElementById("send").innerHTML = "Send: " + emailElmt.value;
    } else {
        document.getElementById("send").innerHTML = ""
    }
}
input[type=email]#email {
   "text-transform: lowercase
}
<!DOCTYPE html>
<html>
<body>
<h3>Client Side to Server Side - Simple Email validation!</h3>
<input type="email" name="email" id="email" pattern="[a-zA-Z0-9._%+-]+@[a-z0-9.-]+\.[a-zA-Z]{2,4}" placeholder="enter email here ..." required oninput="lower()" />

<p id="entered">Entered:</p>
<p id="send">Send:</p>

</body>
</html>

灰色世界里的红玫瑰 2024-11-07 00:50:53
<input type="email" pattern="^[^ ]+@[^ ]+\.[a-z]{2,6}$">

演示 - 测试电子邮件输入

<input type="email" pattern="^[^ ]+@[^ ]+\.[a-z]{2,6}$">

Demo - Test the email input

七禾 2024-11-07 00:50:53

您可能想要这样的东西。请注意属性:

  • 必需的
  • type=email
  • 自动对焦
  • 模式

[电子邮件受保护]" 自动对焦需要模式="[^ @]*@[^ @]*" /> ;

You probably want something like this. Notice the attributes:

  • required
  • type=email
  • autofocus
  • pattern

<input type="email" value="" name="EMAIL" id="EMAIL" placeholder="[email protected]" autofocus required pattern="[^ @]*@[^ @]*" />

难如初 2024-11-07 00:50:53

我使用以下正则表达式来满足以下电子邮件的要求。

[email protected] # Minimum three characters
[email protected] # Accepts Caps as well.
[email protected] # Accepts . before @

代码

<input type="email" pattern="[A-Za-z0-9._%+-]{3,}@[a-zA-Z]{3,}([.]{1}[a-zA-Z]{2,}|[.]{1}[a-zA-Z]{2,}[.]{1}[a-zA-Z]{2,})" />

I used following Regex to satisfy for following emails.

[email protected] # Minimum three characters
[email protected] # Accepts Caps as well.
[email protected] # Accepts . before @

Code

<input type="email" pattern="[A-Za-z0-9._%+-]{3,}@[a-zA-Z]{3,}([.]{1}[a-zA-Z]{2,}|[.]{1}[a-zA-Z]{2,}[.]{1}[a-zA-Z]{2,})" />
最终幸福 2024-11-07 00:50:53

另一种基于 w3org 规范构建的解决方案。
原始正则表达式取自 w3org
此正则表达式中的最后一个“* 惰性量词”已替换为“+ 一个或多个量词”。
这种模式完全符合规范,但有一个例外:它不允许顶级域地址,例如“foo@com

<input
    type="email" 
    pattern="[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)+"
    title="[email protected]"
    placeholder="[email protected]"
    required>

One more solution that is built on top of w3org specification.
Original regex is taken from w3org.
The last "* Lazy quantifier" in this regex was replaced with "+ One or more quantifier".
Such a pattern fully complies with the specification, with one exception: it does not allow top level domain addresses such as "foo@com"

<input
    type="email" 
    pattern="[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)+"
    title="[email protected]"
    placeholder="[email protected]"
    required>
余生再见 2024-11-07 00:50:53

更新的 2018 年答案

转到此处 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,}))$/

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,}))$/

笔芯 2024-11-07 00:50:53
<input type="email" name="email" id="email" value="" placeholder="Email" required />

文档 http://www.w3.org/TR/html-markup/输入.email.html

<input type="email" name="email" id="email" value="" placeholder="Email" required />

documentation http://www.w3.org/TR/html-markup/input.email.html

静若繁花 2024-11-07 00:50:53

以下正则表达式模式应该适用于大多数电子邮件,包括俄语电子邮件。

[^@]+@[^\.]+\..+

The following regex pattern should work with most emails, including russian emails.

[^@]+@[^\.]+\..+

何以畏孤独 2024-11-07 00:50:53

使用 Html5 和 Angular 的电子邮件验证正则表达式如下

<div class="item">
      <p>Email Address<span class="required">*</span></p>
      <input type="email" name="to" id="to" placeholder="Email address" tabindex="6" required
        [(ngModel)]="pancard.email" #to="ngModel" pattern="[a-zA-Z0-9._-]*@[a-zA-Z]*\.[a-zA-Z]{2,3}" />
        <div class="alertField" *ngIf="to.invalid && (to.dirty || to.touched)">
          <div *ngIf="to.errors?.['required']">Email is required </div>
          <div *ngIf="to.errors?.['pattern']">Invalid Email Id.</div>
        </div>
    </div>

这是工作示例..

Email Validation Regex using Html5 with angular as below

<div class="item">
      <p>Email Address<span class="required">*</span></p>
      <input type="email" name="to" id="to" placeholder="Email address" tabindex="6" required
        [(ngModel)]="pancard.email" #to="ngModel" pattern="[a-zA-Z0-9._-]*@[a-zA-Z]*\.[a-zA-Z]{2,3}" />
        <div class="alertField" *ngIf="to.invalid && (to.dirty || to.touched)">
          <div *ngIf="to.errors?.['required']">Email is required </div>
          <div *ngIf="to.errors?.['pattern']">Invalid Email Id.</div>
        </div>
    </div>

It is working example..

与酒说心事 2024-11-07 00:50:53

我测试了以下正则表达式,它给出了与 Chrome Html 电子邮件输入验证相同的结果。

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

你可以在这个网站上测试一下:
regex101

I have tested the following regex which gives the same result as Chrome Html email input validation.

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

You can test it out on this website:
regex101

中性美 2024-11-07 00:50:53
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
梦初启 2024-11-07 00:50:53
pattern="[a-z0-9._%+-]{1,40}[@]{1}[a-z]{1,10}[.]{1}[a-z]{3}"

<input type="email"  class="form-control" id="driver_email" placeholder="Enter Driver Email" name="driver_email" pattern="[a-z0-9._%+-]{1,40}[@]{1}[a-z]{1,10}[.]{1}[a-z]{3}" required="">
pattern="[a-z0-9._%+-]{1,40}[@]{1}[a-z]{1,10}[.]{1}[a-z]{3}"

<input type="email"  class="form-control" id="driver_email" placeholder="Enter Driver Email" name="driver_email" pattern="[a-z0-9._%+-]{1,40}[@]{1}[a-z]{1,10}[.]{1}[a-z]{3}" required="">
别在捏我脸啦 2024-11-07 00:50:53

试试这个

pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"

Try this

pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+
quot;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文