Regex to allow A-Z, - and '

后端 未结 6 2020
情歌与酒
情歌与酒 2021-01-16 21:25

I am trying to get this Regex to work to validate a Name field to only allow A-Z, \' and -.

So far I am using this which is working fine apart from it wont allow an

6条回答
  •  有刺的猬
    2021-01-16 21:47

    if (preg_match("/^[A-Z'-]+$/",$firstname)) {
        // do something
    }
    

    The caret ^ inside a character class [] will negate the match. The way you have it, it means if the $firstname contains characters other than a-z, A-Z, ', and -.

提交回复
热议问题