du5407 2017-07-08 07:51
浏览 364
已采纳

在PHP中使用正则表达式进行用户名验证

I am trying to validate a USERNAME in PHP using Regular Expression. But I am failed. My pattern is: /[^a-z0-9_]/

Rules:

  • username must start number or small letters
  • username support number, small letters and _
  • the end of username character is not be _
  • 写回答

3条回答 默认 最新

  • doumao8803 2017-07-08 08:04
    关注

    The following pattern will work: ^[a-z0-9][a-z0-9_]*[a-z0-9]$

    • ^[a-z0-9]: first character may not be an underscore
    • [a-z0-9_]*: all the others may be anything...
    • [a-z0-9]$: ...except the last one which can't be an underscore.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?