Sign in to follow this  
Followers 0
ray306

Help on regex

5 posts in this topic

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Its because it will find a match on the first part even though is a 4 digit behind it or anything else, you have to use the $ to mark make sure its the end of the line something like

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$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.

Share this post


Link to post
Share on other sites

Thanks folks, Works like a charm....I clearly didn't understand the use of $

Ray306

Share this post


Link to post
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
Sign in to follow this  
Followers 0