home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a067 / 1.img / GRUMP501.EXE / POSTCODE.PRG < prev    next >
Encoding:
Text File  |  1991-04-23  |  3.4 KB  |  80 lines

  1. /*
  2.     Program: IsPostCode()
  3.     System: GRUMPFISH LIBRARY
  4.     Author: Greg Lief
  5.     Copyright (c) 1988-90, Greg Lief
  6.     Clipper 5.0 Version
  7.     Compile instructions: clipper postcode /n/w/a
  8.  
  9.     Provide basic validation for Canadian postal codes
  10.        1) verify that length is seven
  11.        2) verify that format is X#X #X#
  12.        3) check first character against array of territory codes
  13.  
  14.     Calls: Err_Msg()      (function in ERRMSG.PRG)
  15. */
  16. function ispostcode(cstate, cpostalcode)
  17. static postcodes_ := { "NFANewfoundland", "NSBNova Scotia", ;
  18.                        "PECPrince Edward Island", "NBENew Brunswick", ;
  19.                        "PQGQuebec (Eastern)", "PQHMontreal", ;
  20.                        "PQJQuebec (Western)", "ONKOntario (Eastern)", ;
  21.                        "ONLOntario (Central)", "ONMToronto", ;
  22.                        "ONNOntario (Southwestern)", "ONPOntario (Northern)", ;
  23.                        "MBRManitoba", "SKSSaskatchewan", "ABTAlberta", ;
  24.                        "BCVBritish Columbia", "NTXNorthwest Territories", ;
  25.                        "YTYYukon" }
  26. local ret_val := .f., mpostcode, ele, ptr, improv, ;
  27.       mnumbers := '0123456789', badletters := 'DFIOQU'
  28. do case
  29.    //───── first check length - must be 7
  30.    case len(trim(cpostalcode)) != 7
  31.       err_msg("Canadian postal codes must be 7 characters long!")
  32.    case ! (isalpha(substr(cpostalcode, 1, 1)) .and. ;
  33.            substr(cpostalcode, 2, 1) $ mnumbers .and. ;
  34.            isalpha(substr(cpostalcode, 3, 1)) .and. ;
  35.            substr(cpostalcode, 4, 1) == [ ] .and. ;
  36.            substr(cpostalcode, 5, 1) $ mnumbers .and. ;
  37.            isalpha(substr(cpostalcode, 6, 1)) .and. ;
  38.            substr(cpostalcode, 7, 1) $ mnumbers)
  39.         err_msg("Canadian postal codes must be of the format 'X#X #X#'")
  40.    otherwise
  41.       //───── the letters D/F/I/O/Q/U cannot appear in the postal code -
  42.       //───── check that first
  43.       if ! upper(substr(cpostalcode, 3, 1)) $ badletters .and. ;
  44.                 ! upper(substr(cpostalcode, 6, 1)) $ badletters
  45.          err_msg("Invalid postal code")
  46.       else
  47.          //───── up to this point everything checks out okay... now make
  48.          //───── sure that the first character matches the geographic area
  49.          ele := ascan(postcodes_, upper(cstate))
  50.          if ele > 0
  51.             //───── only use first word of the province -- i.e., "Ontario"
  52.             //───── rather than "Ontario (eastern)" -- because if they
  53.             //───── entered the wrong first digit, we don't really know
  54.             //───── which section they wanted
  55.             if ( ptr := at('(', postcodes_[ele]) ) > 0
  56.                improv := substr(postcodes_[ele], 4, ptr - 5)
  57.             else
  58.                improv := substr(postcodes_[ele], 4)
  59.             endif
  60.             do while cstate == substr(postcodes_[ele], 1, 2) .and. ;
  61.                      substr(cpostalcode, 1, 1) != substr(postcodes_[ele], 3, 1)
  62.                ele++
  63.             enddo
  64.             if substr(cpostalcode, 1, 1) != substr(postcodes_[ele], 3, 1)
  65.                err_msg("Invalid postal code for " + improv)
  66.             else
  67.                ret_val := .t.
  68.             endif
  69.          else
  70.             err_msg("Invalid province code")
  71.          endif
  72.       endif
  73. endcase
  74. return ret_val
  75.  
  76. * end function IsPostCode()
  77. *--------------------------------------------------------------------*
  78.  
  79. * eof postcode.prg
  80.