Return to Snippet

Revision: 15637
at July 12, 2009 18:42 by Shifter


Updated Code
function fnValidatePostal($mValue, $sRegion = '')
{
  $mValue = strtolower($mValue));
  $sFirst = substr($mValue, 0, 1);
  $sRegion = strtolower($sRegion);

  $aRegion = array(
    'nl' => 'a',
    'ns' => 'b',
    'pe' => 'c',
    'nb' => 'e',
    'qc' => array('g', 'h', 'j'),
    'on' => array('k', 'l', 'm', 'n', 'p'),
    'mb' => 'r',
    'sk' => 's',
    'ab' => 't',
    'bc' => 'v',
    'nt' => 'x',
    'nu' => 'x',
    'yt' => 'y'
  );
	
  if (preg_match('/[abceghjlkmnprstvxy]/', $sFirst) && !preg_match('/[dfioqu]/', $mValue) && preg_match('/^\w\d\w[- ]?\d\w\d$/', $mValue))
  {
    if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
    {
      if (is_array($aRegion[$sRegion]) && in_array($sFirst, $aRegion[$sRegion]))
      {
        return true;
      }
      else if (is_string($aRegion[$sRegion]) && $sFirst == $aRegion[$sRegion])
      {
        return true;
      }
    }
    else if (empty($sRegion))
    {
      return true;
    }
  }

  return false;
}

Revision: 15636
at July 12, 2009 18:26 by Shifter


Updated Code
function fnValidatePostal($mValue, $sRegion = '')
{
  $sValue = strtolower($mValue));
  $sFirst = substr($sValue, 0, 1);
  $sRegion = strtolower($sRegion);

  $aRegion = array(
    'nl' => 'a',
    'ns' => 'b',
    'pe' => 'c',
    'nb' => 'e',
    'qc' => array('g', 'h', 'j'),
    'on' => array('k', 'l', 'm', 'n', 'p'),
    'mb' => 'r',
    'sk' => 's',
    'ab' => 't',
    'bc' => 'v',
    'nt' => 'x',
    'nu' => 'x',
    'yt' => 'y'
  );
	
  if (!preg_match('/[dfioqu]/', $sValue) && preg_match('/[abceghjlkmnprstvxy]/', $sFirst) && preg_match('/^\w\d\w[- ]?\d\w\d$/', $sValue))
  {
    if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
    {
      if (is_array($aRegion[$sRegion]) && in_array($sFirst, $aRegion[$sRegion]))
      {
        return true;
      }
      else if (is_string($aRegion[$sRegion]) && $sFirst == $aRegion[$sRegion])
      {
        return true;
      }
    }
    else if (empty($sRegion))
    {
      return true;
    }
  }

  return false;
}

Revision: 15635
at July 12, 2009 17:32 by Shifter


Updated Code
function fnValidatePostal($mValue, $sRegion = '')
{
  $sValue = preg_replace('/[^a-z0-9]/', '', strtolower($mValue));
  $sFirst = substr($sValue, 0, 1);
  $sRegion = strtolower($sRegion);

  $aRegion = array(
    'nl' => 'a',
    'ns' => 'b',
    'pe' => 'c',
    'nb' => 'e',
    'qc' => array('g', 'h', 'j'),
    'on' => array('k', 'l', 'm', 'n', 'p'),
    'mb' => 'r',
    'sk' => 's',
    'ab' => 't',
    'bc' => 'v',
    'nt' => 'x',
    'nu' => 'x',
    'yt' => 'y'
  );
	
  if (!preg_match('/[dfioqu]/', $sValue) && preg_match('/[abceghjlkmnprstvxy]/', $sFirst) && preg_match('/^\w\d\w\d\w\d$/', $sValue))
  {
    if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
    {
      if (is_array($aRegion[$sRegion]) && in_array($sFirst, $aRegion[$sRegion]))
      {
        return true;
      }
      else if (is_string($aRegion[$sRegion]) && $sFirst == $aRegion[$sRegion])
      {
        return true;
      }
    }
    else if (empty($sRegion))
    {
      return true;
    }
  }

  return false;
}

Revision: 15634
at July 12, 2009 12:25 by Shifter


Initial Code
function fnValidatePostal($mValue)
{
  $sPattern = '/^[a-z]{1}[0-9]{1}[a-z]{1}[- ]?[0-9]{1}[a-z]{1}[0-9]{1}$/i';
  return preg_match($sPattern, $mValue);
}

Initial URL

                                

Initial Description

                                

Initial Title
Validate Canadian Postal Code

Initial Tags
code

Initial Language
PHP