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 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口