Regex to allow A-Z, - and '

后端 未结 6 2054
情歌与酒
情歌与酒 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:46

    From what I see. Following Regex should work fine:

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

    Here I have escaped both apostrophe and dash. I have tested this in an online Regex tester and works just fine.

    Give it a try

提交回复
热议问题