使用电子邮件地址作为用户名的正则表达式? [英] A regular expression for using an email address as username?

查看:122
本文介绍了使用电子邮件地址作为用户名的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用常规用户名或电子邮件地址作为用户名的正则表达式?我希望用户能够输入自己的用户名,或者仅使用他们的电子邮件地址作为用户名,而我却找不到任何可靠的信息来正确实现此目的。它还必须通过验证,例如:如果用户选择创建自己的用户名,则必须遵守我的用户名政策,该政策将用户名限制为以字母或数字开头,并且不能包含特殊字符,或者如果他们输入电子邮件,则必须遵守电子邮件规则(典型的电子邮件规则)。有人对此有建议吗?

解决方案

尝试以下正则表达式:

  / ^(?:[AZ\d] [AZ\d _-] {5,10} | [A-Z0-9 ._%+-] + @ [A-Z0-9 .-] + \。[AZ] {2,4})$ / i 

表达式包含两个部分:



  • 第一部分验证用户名。随意优化此部分以匹配您自己的策略。此处的正则表达式接受以字母或数字开头的用户名。用户名不能超过11个字符。允许使用-和_。



  • 第二部分验证电子邮件。在撰写本文时,此正则表达式



Is there a regular expression out there for using a normal username OR an email address as a username? I would like a user to be able to enter their own username, or just use their email address as the username and I am unable to find any reliable information on how to achieve this properly. It would still have to pass validation as well, for example: if the user chooses to make their own username, it would have to abide by my policy for usernames, which limits them to starting with a letter or number, and no special characters, or if they enter an email, it would have to abide by the email rules(typical email rules). Anyone have a suggestion for this?

解决方案

Try this regular expression:

/^(?:[A-Z\d][A-Z\d_-]{5,10}|[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})$/i

The expression has two parts:

  • The first part validates a username. Feel free to optimize this part for matching you own policy. The regex here accepts username starting with a letter or a number. A username can't belonger than 11 characters. - and '_' are allowed.

  • The second part validates an email. This regex validates 99% of emails in use as of this writing. However, you may use another email regex.

Description

References

这篇关于使用电子邮件地址作为用户名的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆