Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Postal Code or Zip Code 1

Status
Not open for further replies.

wood

MIS
Aug 3, 2000
139
0
0
CA
I am new to perl, and am trying to check if the data entered is a string or numeric. IE if the person entered a Postal Code or Zip Code.

Could someone assist me? Thanks!
 
Code:
my $input = <>;  #get the string
if ($input =~ /^\d+$/)   #checks if the variable consists of   
                         #numbers in its intirety 
{  do something numeric with it }
else
{  treat it like a string  }
 
I'll always follow the rule of "If no calculation on the field is required, define the field as string else numeric".

This will make compairison a lot easier down the road.
 
Thanks azzazzello that worked perfectly! I give you a star!
 
Keep in mind that US codes have 2 valid formats.

12345
and
12345-1234



Michael Libeson
 
Yes, I do realize this, but I am validating from what our system stores, and we only use the first five digits.

Thanks!
 
if they are able to enter the extended format, but you want only the first 5 digits, the code below will help.

Code:
$input = '12345-1234';

if ($input =~ /^([0-9]{5,5})\-[0-9]{4,4}$/) {
     $input = $1;
}

Apply this code before your check:
if ($input =~ /^\d+$/)


Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top