dtnd30892 2011-12-01 07:22
浏览 122
已采纳

用于在'@'之前验证电子邮件长度的正则表达式

An open-source cms I'm working on uses the following regex for validating emails:

 valid_regex=^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$

and I need to validate also the length of the email address before the '@' symbol to accept emails with at least 2 characters.. I've read that using {2,} will do the trick but where and how exactly should I use it?......

  • 写回答

3条回答 默认 最新

  • doushi1974 2011-12-01 08:02
    关注

    Your regex will already force the email to be atleast 2 (or rather 3) characters before the @.

    [_a-z0-9-]+(.[_a-z0-9-]+)
              ^            ^
    

    In both cases you're using the + sign which symbols that the following character should be repeated 1 or many times. Note that this regex will not match several valid email-addresses and have a lot of other problems.

    For a starter you should escape each dot using backslash \. and as it is now you force all addresses to have exactly one dot.

    An easy solution would be to make the dot optional in your current regex:

    [_a-z0-9-]+(\.?[_a-z0-9-]+)+
    

    And I guess that you don't really want to limit the address to have only 1 dot in it. If you Do want that simply remove the last plus sign.

    You can see it in action here: http://regexr.com?2vbof

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?