2013-07-31 122 views
-1

我用这个验证:验证英国短邮政编码

//validate postcode 
function IsPostcode($postcode) 
{ 
    $postcode = strtoupper(str_replace(' ','',$postcode)); 
    if(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

From this link.

但我希望能够验证像ME20和TN10邮政编码,而不是一个完全成熟的ME20 4RN,TN0 4RN的。

有人可以帮助我与正则表达式吗?

+0

http://regular-expressions.info/tutorial.html – deceze

+0

有一个库可以做到这一点https://gist.github.com/ChrisMcKee/4452116只需将incode选项设置为只验证第一部分 – Anigel

+0

你对于正则表达式模式有多少了解?只有最基本的正则表达式知识才能从你已有的起点找出答案。我建议去读[更多关于正则表达式](http://www.regular-expressions.info/),而不是问这样一个基本问题。你会学到更多的东西。 – Spudley

回答

2

,你可以用我的更新正则表达式来解决你的问题

从我结束工作,以验证英国的邮政编码

<?php 
function IsPostcode($postcode) 
{ 
    $postcode = strtoupper(str_replace(' ','',$postcode)); 
    if(preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]?[\s]?[0-9][ABD-HJLNP-UW-Z]{2}$)/i",$postcode) || preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]$)/i",$postcode)) 
    {  
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

echo $result = IsPostcode('ME20'); 
?> 

输出

希望这将肯定帮助你。