dongxin8709 2014-07-05 12:53
浏览 31
已采纳

PHP检查邮政编码

I need to verify the postal code inserted in a text box and stored in a variable: "$code".

iIf the format is like this: XXXX – XXX it's valid. If not, it's invalid (the X's are numbers).

Someone told me to make a regex like this one but applied to my needs:

$regex = "/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/";

Not only do I not know how to make that, but I also don't know what to do after.

Do you guys have any idea on how to solve my problem?

  • 写回答

2条回答 默认 最新

  • dpo60833 2014-07-05 13:01
    关注

    You could try something like :

    /^[0-9]{4}\s-\s[0-9]{3}$/
    

    Or :

    /^\d{4}\s-\s\d{3}$/
    

    EDIT : If you have 0 or more space between the - and the digits :

    /^\d{4}\s*-\s*\d{3}$/
    

    If you have 0 or only one space between the - and the digits :

    /^\d{4}\s?-\s?\d{3}$/
    

    If you haven't any spaces between the - and the digits :

    /^\d{4}-\d{3}$/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化