validate zip code 
Author Message
 validate zip code

How can I validate the entry of a US zipcode into a text field on a form?

I want to be sure that the first 5 characters are numeric, and if the dash
is used for the 9-digit zipcodes, that the dash is in the 6th position, and
if the other four digits are entered, that they are in fact digits.

I don't like the built-in input mask, because for 5-digit zip codes, it
leaves in the dash!

Thanks.

--
Bill

PhotoSourceFolio webmaster
http://www.*-*-*.com/



Tue, 25 Nov 2003 09:48:54 GMT  
 validate zip code
Bill,

Public Function ValidateZip(sCode as String) As Boolean
    ValidateZip = False

    If Len(sCode) = 5 Then
        If IsNumeric(sCode) Then ValidateZip = True
    If Len(sCode) = 9 Then
        If IsNumeric(sCode) Then ValidateZip = True
    ElseIf Len(sCode) = 10 Then
        If Instr(1,sCode,"-") = 6 Then
            If IsNumeric(Left(sCode,5)) Then
                If IsNumeric(Right(sCode,4)) Then ValidateZip = True
            End If
        End If
    End If
End Function

------
Graham R Seach MCP Microsoft Access

------


Quote:
> How can I validate the entry of a US zipcode into a text field on a form?

> I want to be sure that the first 5 characters are numeric, and if the dash
> is used for the 9-digit zipcodes, that the dash is in the 6th position,
and
> if the other four digits are entered, that they are in fact digits.

> I don't like the built-in input mask, because for 5-digit zip codes, it
> leaves in the dash!

> Thanks.

> --
> Bill

> PhotoSourceFolio webmaster
> http://www.photosourcefolio.com



Tue, 25 Nov 2003 11:58:55 GMT  
 validate zip code
On Fri, 8 Jun 2001 13:58:55 +1000, "Graham R Seach"

Quote:

>Public Function ValidateZip(sCode as String) As Boolean
>    ValidateZip = False

>    If Len(sCode) = 5 Then
>        If IsNumeric(sCode) Then ValidateZip = True
>    If Len(sCode) = 9 Then

     Elseif Len(sCode) = 9 Then

Quote:
>        If IsNumeric(sCode) Then ValidateZip = True
>    ElseIf Len(sCode) = 10 Then
>        If Instr(1,sCode,"-") = 6 Then
>            If IsNumeric(Left(sCode,5)) Then
>                If IsNumeric(Right(sCode,4)) Then ValidateZip = True
>            End If
>        End If
>    End If
>End Function

? ValidateZip("95D30")
True

Bzzzt.  But thank you for playing.  You'll receive a home version of
our game.

--
Mike Sherrill
Information Management Systems



Tue, 25 Nov 2003 23:34:34 GMT  
 validate zip code
Good call, but I wonder if you knew what I meant...

Public Function ValidateZip(sCode As Variant) As Boolean

------
Graham R Seach MCP Microsoft Access

------


Quote:
> On Fri, 8 Jun 2001 13:58:55 +1000, "Graham R Seach"

> >Public Function ValidateZip(sCode as String) As Boolean
> >    ValidateZip = False

> >    If Len(sCode) = 5 Then
> >        If IsNumeric(sCode) Then ValidateZip = True
> >    If Len(sCode) = 9 Then

>      Elseif Len(sCode) = 9 Then

> >        If IsNumeric(sCode) Then ValidateZip = True
> >    ElseIf Len(sCode) = 10 Then
> >        If Instr(1,sCode,"-") = 6 Then
> >            If IsNumeric(Left(sCode,5)) Then
> >                If IsNumeric(Right(sCode,4)) Then ValidateZip = True
> >            End If
> >        End If
> >    End If
> >End Function

> ? ValidateZip("95D30")
> True

> Bzzzt.  But thank you for playing.  You'll receive a home version of
> our game.

> --
> Mike Sherrill
> Information Management Systems



Wed, 26 Nov 2003 01:43:01 GMT  
 validate zip code
On Sat, 9 Jun 2001 03:43:01 +1000, "Graham R Seach"

Quote:

>Good call, but I wonder if you knew what I meant...

>Public Function ValidateZip(sCode As Variant) As Boolean

Did you mean for your code to do this?

  >I want to be sure that the first 5 characters are numeric,
  >and if the dash is used for the 9-digit zipcodes, that
  >the dash is in the 6th position, and if the other four
  >digits are entered, that they are in fact digits.

If not, then I didn't know what you meant.  Still don't. <g>

--
Mike Sherrill
Information Management Systems



Wed, 26 Nov 2003 06:22:53 GMT  
 validate zip code
Bill,

It seems that Access evaluates the letters "d" and "e" as mathmatical
operators, so when used within what would otherwise be a number, they
constitute a mathematical expression which results in a valid number. This
is not something I had considered.

I have tested the following code, and it works.

Public Function ValidateZip(sCode As String) As Boolean
    Dim ctr As Integer, tmp As String, tmp1 As String

    ValidateZip = True
    tmp = Replace(sCode, "-", "")

    'Check for chars
    For ctr = 1 To Len(tmp)
        tmp1 = Mid(tmp, ctr, 1)

        If Not (IsNumeric(tmp1)) Then
            ValidateZip = False
            Exit Function
        End If
    Next ctr

    tmp = sCode

    'Check for length and hyphen
    Select Case Len(tmp)
        Case 5, 9: 'No action
        Case 10: If InStr(1, sCode, "-") <> 6 Then ValidateZip = False
        Case Else: ValidateZip = False
    End Select
End Function

------
Graham R Seach MCP Microsoft Access

------


Quote:
> How can I validate the entry of a US zipcode into a text field on a form?

> I want to be sure that the first 5 characters are numeric, and if the dash
> is used for the 9-digit zipcodes, that the dash is in the 6th position,
and
> if the other four digits are entered, that they are in fact digits.

> I don't like the built-in input mask, because for 5-digit zip codes, it
> leaves in the dash!

> Thanks.

> --
> Bill

> PhotoSourceFolio webmaster
> http://www.photosourcefolio.com



Fri, 28 Nov 2003 14:43:23 GMT  
 validate zip code
It's always something, ain't it?

Thanks.  I'll try out the new code.

--
Bill

PhotoSourceFolio webmaster
http://www.photosourcefolio.com



Quote:
> Bill,

> It seems that Access evaluates the letters "d" and "e" as mathmatical
> operators, so when used within what would otherwise be a number, they
> constitute a mathematical expression which results in a valid number. This
> is not something I had considered.

> I have tested the following code, and it works.

> Public Function ValidateZip(sCode As String) As Boolean
>     Dim ctr As Integer, tmp As String, tmp1 As String

>     ValidateZip = True
>     tmp = Replace(sCode, "-", "")

>     'Check for chars
>     For ctr = 1 To Len(tmp)
>         tmp1 = Mid(tmp, ctr, 1)

>         If Not (IsNumeric(tmp1)) Then
>             ValidateZip = False
>             Exit Function
>         End If
>     Next ctr

>     tmp = sCode

>     'Check for length and hyphen
>     Select Case Len(tmp)
>         Case 5, 9: 'No action
>         Case 10: If InStr(1, sCode, "-") <> 6 Then ValidateZip = False
>         Case Else: ValidateZip = False
>     End Select
> End Function

> ------
> Graham R Seach MCP Microsoft Access

> ------



> > How can I validate the entry of a US zipcode into a text field on a
form?

> > I want to be sure that the first 5 characters are numeric, and if the
dash
> > is used for the 9-digit zipcodes, that the dash is in the 6th position,
> and
> > if the other four digits are entered, that they are in fact digits.

> > I don't like the built-in input mask, because for 5-digit zip codes, it
> > leaves in the dash!

> > Thanks.

> > --
> > Bill

> > PhotoSourceFolio webmaster
> > http://www.photosourcefolio.com



Sat, 29 Nov 2003 00:47:32 GMT  
 validate zip code
On Mon, 11 Jun 2001 16:43:23 +1000, "Graham R Seach"

Quote:

>I have tested the following code, and it works.

[snip]

 ? ValidateZip("00000-0--0")
 True

BZZZT. <g>

--
Mike Sherrill
Information Management Systems



Sat, 29 Nov 2003 01:33:10 GMT  
 validate zip code
Previously I was going to suggest:

    Function ValidateZip(testvalue As String) As Boolean

        ValidateZip = True

        If testvalue Like "#####" Then
            Exit Function
        ElseIf testvalue Like "#####-####" Then
            Exit Function
        ElseIf testvalue Like "#########" Then
            Exit Function
        ElseIf testvalue Like "#####-" Then  'okay by me
            Exit Function
        Else
            ValidateZip = False
        End If

    End Function

but it was a bit slower than the suggested versions.  If those versions have
to add more complicated testing code, though, maybe this approach is worth a
second look.

--

Dirk Goldgar
www.datagnostics.com
(to reply via email, remove NOSPAM from address)


Quote:
>On Mon, 11 Jun 2001 16:43:23 +1000, "Graham R Seach"

>>I have tested the following code, and it works.
>[snip]

> ? ValidateZip("00000-0--0")
> True

>BZZZT. <g>

>--
>Mike Sherrill
>Information Management Systems



Sat, 29 Nov 2003 03:08:48 GMT  
 validate zip code
Bill,

I think Dirk's code is the way to go. It's clean, simple, and it works!

Posting alternative code instead of smart comments is a much better approach
to solving someone's problem, don't you think? (Actually, I think I'm
beginning to like this Mike character - now that's a worry!)

------
Graham R Seach MCP Microsoft Access

------


Quote:
> Previously I was going to suggest:

>     Function ValidateZip(testvalue As String) As Boolean

>         ValidateZip = True

>         If testvalue Like "#####" Then
>             Exit Function
>         ElseIf testvalue Like "#####-####" Then
>             Exit Function
>         ElseIf testvalue Like "#########" Then
>             Exit Function
>         ElseIf testvalue Like "#####-" Then  'okay by me
>             Exit Function
>         Else
>             ValidateZip = False
>         End If

>     End Function

> but it was a bit slower than the suggested versions.  If those versions
have
> to add more complicated testing code, though, maybe this approach is worth
a
> second look.

> --

> Dirk Goldgar
> www.datagnostics.com
> (to reply via email, remove NOSPAM from address)



> >On Mon, 11 Jun 2001 16:43:23 +1000, "Graham R Seach"

> >>I have tested the following code, and it works.
> >[snip]

> > ? ValidateZip("00000-0--0")
> > True

> >BZZZT. <g>

> >--
> >Mike Sherrill
> >Information Management Systems



Sat, 29 Nov 2003 07:52:40 GMT  
 validate zip code
On Tue, 12 Jun 2001 09:52:40 +1000, "Graham R Seach"

Quote:

>Posting alternative code instead of smart comments is a much better approach
>to solving someone's problem, don't you think?

Hey, you're the MCP.  :)  

Besides, sometimes I have barely enough time for wisecracks.  

--
Mike Sherrill
Information Management Systems



Sun, 30 Nov 2003 06:29:31 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. abcrd001.zip/abc0995.zip - ALL BASIC CODE Packets/Reader/Extractor

2. Need ZIP source code or freeware ZIP DLL

3. validate city, state, and zip

4. ANN: Zip Code/Area Code/Time Zone Data and Components

5. Postal codes and zip codes

6. international post codes (Zip codes) - ricerca tabella codici postali europei per database

7. How to validate text box contents in validating event as well as assignment

8. Form's validate does not fire usercontrol(inside textbox) validating event

9. Validating/Validated questions

10. Validating, Validated, LostFocus Event Order

11. Validate Code - Why doesn't this work!?

12. code for validating or resolving email addresses

 

 
Powered by phpBB® Forum Software