doulian5857 2018-03-19 12:39
浏览 134
已采纳

用于验证名字和姓氏的PHP正则表达式[重复]

This question already has an answer here:

I am trying to use regex to validate the users first and last name on a form.

Rules

  • Must be alphabetic
  • First letter can either by uppercase or lowercase
  • Followed by all lowercase letters
  • Must be at least 2 letters long and cannot be longer than 15 letters.
  • No spaces.

This is my attempt at the regular expression

!preg_match('/^[a-Z]{1}[a-z]{1,14}$/' 

But I am getting the error:

: preg_match(): Compilation failed: range out of order in character class at offset 4

</div>
  • 写回答

1条回答 默认 最新

  • douliandan7340 2018-03-19 12:42
    关注

    a-Z isn't a valid range, you need to change this to include both uppercase and lowercase ranges.

    change to this: ^[a-zA-Z][a-z]{1,14}$

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?