Announcement

Collapse
No announcement yet.

string validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • string validation

    A list of state abbreviations is at http://www.usps.com/ncsc/lookups/abbr_state.txt
    Assuming it has already been checked for a length of 2, what would be the most efficient way of checking that a string is a valid state abbreviation?
    Erich Schulman (KT4VOL/KTN4CA)
    Go Big Orange

  • #2
    Put the abbreviations in an array and do a binary search on it, or, put all of the abbreviations in a string and do an INSTR search. For example:

    Code:
    sAllStates = " AL AK AS AZ AR CA"  '..... etc...
    
    If Instr( sAllStates, $Spc & sLookFor ) Then
       ' the state abbreviation was found. Do something here.
    Else
       ' Not found. Weep openly.
    End If
    Paul Squires
    FireFly Visual Designer (for PowerBASIC Windows 10+)
    Version 3 now available.
    http://www.planetsquires.com

    Comment


    • #3
      Put all the state abbreviations in a long string with some kind of separator, (space, comma, whatever) and use INSTR?

      Well, Paul and I posted at the same time.
      There are no atheists in a fox hole or the morning of a math test.
      If my flag offends you, I'll help you pack.

      Comment


      • #4
        Put all the state abbrev in a string (be sure they are all correct and 2 characters each).

        Do an instr for checking and compare the position. If it is odd, it is ok, if even,
        check again. If 0, it is bad.
        Code:
        #COMPILE EXE
        '#DIM ALL
        
        FUNCTION PBMAIN () AS LONG
        statest$="ALAKAZARCACOCTDEFLGAHIIDILINIAKSKYLAMEMDMAMIMNMSMOMTNENVNHNJNMNYNCNDOHOKORPARISCSDTNTXUTVTVAWAWVWIWY"
        PRINT "len(statest$)= ";LEN(statest$)
        L%=INSTR(statest$,"OK")
        IF L% MOD 2=1 THEN ok%=1 :showstate%=L% ELSE ok%=0
        IF L%<>0 AND L% MOD 2=0 THEN
         M%=INSTR( L%+1,statest$,"OK")
         IF M% MOD 2=1 THEN OK%=1:showstate%=M%
         IF M%=0 THEN ok%=0
        END IF
        PRINT "OK%= ";ok%
        
        IF OK% THEN
          SELECT CASE showstate%
            CASE 1
              PRINT "ALABAMA  AL"
            CASE 3
              PRINT "ALASKA   AK"
            CASE 5
              PRINT "ARIZONA  AZ"
            CASE 7
              PRINT "ARKANSAS AR"
            CASE 9
              PRINT "CALIFORNIA  CA"
            CASE 11
              PRINT "COLORADO CO"
            CASE 13
              PRINT "CONNECTICUT CT"
            CASE 15
              PRINT "DELAWARE  DE"
            CASE 17
              PRINT "FLORIDA   FL"
            CASE 19
              PRINT "GEORGIA   GA"
            CASE 21
              PRINT "HAWAII  HI"
            CASE 23
              PRINT "IDAHO ID"
            CASE 25
              PRINT "ILLINOIS  IL"
            CASE 27
              PRINT "INDIANA  IN"
            CASE 29
              PRINT "IOWA  IA"
            CASE 31
              PRINT "KANSAS   KS"
            CASE 33
              PRINT "KENTUCKY  KY"
            CASE 35
              PRINT "LOUISIANA  LA"
            CASE 37
              PRINT "MAINE  ME"
            CASE 39
              PRINT "MARYLAND MD"
            CASE 41
              PRINT "MASSACHUSETTS MA"
            CASE 43
              PRINT "MICHIGAN MI"
            CASE 45
              PRINT "MINNESOTA MN"
            CASE 47
              PRINT "MISSISSIPPI MS"
            CASE 49
              PRINT "MISSOURI MO"
            CASE 51
              PRINT "MONTANA MT"
            CASE 53
              PRINT "NEBRASKA NE"
            CASE 55
              PRINT "NEVADA NV"
            CASE 57
              PRINT "NEW HAMPSHIRE NH"
            CASE 59
              PRINT "NEW JERSEY NJ"
            CASE 61
              PRINT "NEW MEXICO NM"
            CASE 63
              PRINT "NEW YORK NY"
            CASE 65
              PRINT "NORTH CAROLINA NC"
            CASE 67
              PRINT "NORTH DAKOTA ND"
            CASE 69
              PRINT "OHIO OH"
            CASE 71
              PRINT "OKLAHOMA OK"
            CASE 73
              PRINT "OREGON OR"
            CASE 75
              PRINT "PENNSYLVANIA PA"
            CASE 77
              PRINT "RHODE ISLAND RI"
            CASE 79
              PRINT "SOUTH CAROLINA SC"
            CASE 81
              PRINT "SOUTH DAKOTA SD"
            CASE 83
              PRINT "TENNESSEE TN"
            CASE 85
              PRINT "TEXAS TX"
            CASE 87
              PRINT "UTAH UT"
            CASE 89
              PRINT "VERMONT VT"
            CASE 91
              PRINT "VIRGINIA VA"
            CASE 93
              PRINT "WASHINGTON WA"
            CASE 95
              PRINT "WEST VIRGINIA WV"
            CASE 97
              PRINT "WISCONSIN WI"
            CASE 99
              PRINT "WYOMING WY"
          END SELECT
        END IF
        WAITKEY$
        
        
        END FUNCTION
        Last edited by Fred Buffington; 13 Jan 2008, 03:57 AM.
        Client Writeup for the CPA

        buffs.proboards2.com

        Links Page

        Comment


        • #5
          Also you may want to account for capitalization and the non-50-state codes:

          Code:
          #COMPILE EXE
          #DIM ALL
          
          FUNCTION PBMAIN () AS LONG
              LOCAL validStates, stateStr AS STRING
              
              validStates = " AL AK AS AZ AR CA CO CT DE DC FM FL GA GU HI ID IL IN IA KS KY LA ME MH MD MA MI MN MS MO MT NE" & _
                            " NV NH NJ NM NY NC ND MP OH OK OR PW PA PR RI SC SD TN TX UT VT VI VA WA WV WI WY AE AA AE AE AE AP"
              stateStr = "ne"
              stateStr = " " & UCASE$(stateStr)
              
              IF INSTR(validStates, stateStr) THEN
                 ? stateStr & " is a valid state abbreviation"
              ELSE
                 ? stateStr & " is NOT a valid state abbreviation"
              END IF
          END FUNCTION
          Last edited by John Gleason; 13 Jan 2008, 09:18 AM.

          Comment


          • #6
            Code:
            REDIM TheStates (49)
            FOR Z = 0 To 49
              TheStates(Z) = READ$(Z+1)
            NEXT
            
              StateToTest = "XX"
            
              ARRAY SCAN TheStates(), = StateToTest,to iHit
              IF ISFALSE iHit THEN
                MSGBOX "Invalid State.."
            
            
            
            DATA AL,AK,AR....
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment

            Working...
            X