dongyingjiu0669 2012-11-08 16:45
浏览 130
已采纳

preg_match永远不会通过

My account creation script's name input is validated with a preg_match as the field is only allowed to have either Latin or Cyrillic characters (in other words, it cannot have both), however, no matter what the name field contains, the preg_match condition is never satisfied. So far I've tried three possible variations of the code:

if (!(preg_match('/^[a-z]$/i',$_POST['name']) || preg_match('/^[а-я]$/i',$_POST['name']))) back('The message');

if (!preg_match('/^[a-z]$/i',$_POST['name']) &&  !preg_match('/^[а-я]$/i',$_POST['name'])) back('The message');

and one that attempts to stuff both the preg matches into one.

Could anyone please help me out in fixing this issue?

  • 写回答

4条回答 默认 最新

  • douzhuo6931 2012-11-08 16:50
    关注

    The problem is that you are limiting the name to exactly one character:

    /^[a-z]$/i
    

    Change it to:

    /^[a-z]+$/i
    

    and

    /^[а-я]+$/i
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?