How to validate a user name with regex?

后端 未结 10 1725
礼貌的吻别
礼貌的吻别 2020-11-27 12:34

This seems to match the rules I have defined, but I only starting learning regex tonight, so I am wondering if it is correct.

Rules:

  • Usernames can cons
10条回答
  •  失恋的感觉
    2020-11-27 13:24

    By the looks of it, that rule wouldn't match something like "a_bc", "ab_c", "a_b" or "a_b_c".

    Try: /^[a-zA-Z0-9]+([_\s\-]?[a-zA-Z0-9])*$/ which matches the above cases but not any combination of spaces, dashes or underscores next to each other. Eg: "_-" or " _" are not allowed.

提交回复
热议问题