FMDiff™   

FileMaker Business Alliance
View Jürgen Geßwein's profile on LinkedIn

This site is W3C compliant:
Valid XHTML - Valid CSS
Last modified February 24 2016, 19:25:31 CET.

Checking the Canadian Postcode Format

The question that triggered this entry was: How to go to the next field as soon as the canadian postcode is entered. As always there is more than one solution. I describe here what I think is the best one.

First please read Wikipedia about the format definition.

Please note: although the british postcode looks very much like the canadian, it followes completely different rules. Hence this solution can't be used there!


We split the task in two parts, verify the format and Go to Field.

For common usability we create a Custom Function "CanadianZip" that is defined as follows:

Function Name and Parameters:

CanadianZip ( p )   // may be entered in upper or lower case, with or without a blank.
It returns a correctly formatted postcode or an empty string in case of an error.

Function Definition:

Let ( [
    p = Upper ( p ) ;
    a = "ABCEGHJKLMNPRSTVWXYZ" ;    // do not include the letters D, F, I, O, Q or U.
    n = "1234567890" ;
    p = Filter ( p ; a & n ) ;      // remove blanks and illegal characters
    c = Middle ( p ; 1 ; 1 ) & Middle ( p ; 3 ; 1 ) & Middle ( p ; 5 ; 1 ) ;  // alpha chars
    d = Middle ( p ; 2 ; 1 ) & Middle ( p ; 4 ; 1 ) & Middle ( p ; 6 ; 1 )    // numbers
  ] ;
  If ( Length ( Filter ( c ; a ) & Filter ( d ; n ) ) = 6 and not
      Position ( "WZ" ; Left ( c ; 1 ) ; 1 ; 1 ) ;   // W and Z may not be at pos 1
    Left ( p ; 3) & " " & Right ( p ; 3 ) ;          // insert a space
    "" )                                             // or return an empty string
  )


Use this Custom Function in a Script

The original task was to go to a different field, once the postcode has been entered correctly. so we need a script:

# We need a regular field to enter the postcode, for example
#  to::postcode
#  That field must have an onModifyElement event, calling our script

Enter Postcode

Set Variable [$p ; CanadianZip ( to::postcode )]
If ( not isempty ( $p )
  # replace entry with formatted postcode
  to::postcode = $p
  Go to Field [ to::next ]
Else
  # indicate the postcode is not OK somehow
End if

 

 

Any comments are welcome


Examples are provided "AS IS" without warranties of any kind. Use at your own risk.

© 2005 - 2015 Winfried Huslik †. © 2024 Jürgen Geßwein. All Rights Reserved. FMDiff and FMVis are trademarks of Jürgen Geßwein, Augsburg, Germany. FileMaker is a trademark of FileMaker Inc., Santa Clara, CA, USA. Other trademarks mentioned are property of their respective owners. This web site has not been authorised, sponsored, or otherwise approved by FileMaker, Inc.