Jump to content

Help on regex


ray306
 Share

Recommended Posts

Hi:

I'm trying to validate a U.S. zip code in either 5digit or zip+4 format

It appears to me to be accurate but it is not working. Here is the code:

CODE
$_zip = GUICtrlRead ($Czip )

If $_zip = "" Then;Means this is a required field

$errStr = $errStr & "Zip is a Required Field"&@cr

$errCount += 1

GUICtrlSetColor($CLabel7, 0xff0000)

ElseIf Not (StringRegExp ( $_zip , "[0-9]{5}|([0-9]{5}[\-]{1}[0-9]{4})" ,0)) Then

$errStr = $errStr & " Invalid Zip. Use Either NNNNN or NNNNN-NNNN Format"&@cr

GUICtrlSetColor($Czip, 0xff0000)

$errCount += 1

Endif

Any ideas?

Thanks

Ray306

Link to comment
Share on other sites

Hi,

should be easy.

You have a string and want to check whether it is a correct zip code?

I dunno the USA zip codes how do correct ones look?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Moderators

$sString = "15697"
If _IsValidZip($sString) Then MsgBox(64, 'Info1', 'Works')
$sString = "15697-2345"
If _IsValidZip($sString) Then MsgBox(64, 'Info2', 'Works')

Func _IsValidZip($sString)
    Return StringRegExp($sString, "^\d{5}(\s*[\-]\s*\d{4})?$")
EndFunc
Edit:

Took care of a possible space issue between the hyphen and numbers of zip.

Edit2:

Edited the edit, I didn't notice you were already using GUICtrlRead() :whistle:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...