简体   繁体   English

文本框仅接受一个@

[英]Textbox to accept only one @

I'm creating an email field in my html, can I only accept one @ ? 我正在html中创建一个电子邮件字段,我只能接受一个@吗?

For example email: 例如电子邮件:

chong!#$@gmail.com - should invalid because of there are others special characters included chong!#$@gmail.com应该无效,因为其中包含其他特殊字符

or 要么

ch@ng@gmail.com - should also be invalid because there are two @ 's. ch@ng@gmail.com也应该无效,因为有两个@

The only accepted special character should only be one @ , how do I do this in javascript/jquery? 唯一接受的特殊字符只能是一个@ ,如何在javascript / jquery中做到这一点? Sorry I really don't know much in regex. 抱歉,我真的对正则表达式了解不多。 Or is there another way to validate an email format? 还是有另一种方法来验证电子邮件格式?

您可以在输入中使用以下正则表达式:

 <input type="email" pattern="[a-zA-Z0-9.]+\@[a-zA-Z0-9.]+\.[a-zA-Z]+" />

This pattern avoid the user input an 'email' that don't fits with the email standard but also avoid limited the number of characters input in the name of user to 64 characters and the number of characters in the domain too. 这种模式可以避免用户输入不符合电子邮件标准的“电子邮件”,但是也可以避免将用户名中输入的字符数限制为64个字符,并且将域中的字符数也限制为。

  • ^[A-Z0-9._%+-]{1,64}@(?:[A-Z0-9-]{1,63}.){1,125}[AZ]{2,63}$ ^ [A-Z0-9 ._%+-] {1,64} @(?:[A-Z0-9-] {1,63}。){1,125} [AZ] {2,63} $

Some other patterns for validate numbers, numbers and letters and just letters: 验证数字,数字和字母以及仅字母的其他一些模式:

  • ^[0-9]+$ ^ [0-9] + $
  • ^[a-zA-Z0-9]+$ ^ [a-zA-Z0-9] + $
  • ^[a-zA-Z]+$ ^ [a-zA-Z] + $

Also you can use regular expression with javascript like this Validate email address in JavaScript? 您还可以将正则表达式与javascript一起使用,例如在JavaScript中验证电子邮件地址吗? and this other page its really useful for check if your regex pattern works correctly http://regexr.com/ 这另一页对于检查您的正则表达式模式是否正常工作非常有用http://regexr.com/

Try using this. 尝试使用这个。 It will open up a popup explaining the error if format is incorrect: 如果格式不正确,它将打开一个弹出窗口,解释错误:

<form>
    <input pattern="[a-zA-Z0-9.]+\@[a-zA-Z0-9.]+\.[a-zA-Z]+" title="Write your error here" />
    <input type="submit" />
</form>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM