Perl script :- Phone number validation

Hi All,

I am doing a perl script validation for Phone numbers.
The normal phone number format is 01-32145.

I need to do two validations for the phone number

1) A valid phone number can have at least two digits as prefix and at least five digits as postfix. e.g. 01-01011

2) A valid phnoe number can't contain only the digit "0" in either area code or phone number e.g. 00-11111 or 01-00000 is not allowed.

I am able to do the first validation, but not the second ..

Can anybody help me to do the second validation ???

Thanks in advance

Regards,
Bala

use below:-

echo "aaa mmm 00-11113 hhh" | perl -wlane ' /\b\d{2,}\-\d{5,}\b/ and $phone=$&; @nums=(split /-/,$phone) ;
if (int($nums[0])==0 || int($nums[1]==0)){print "Phone number not valid"} ; '

if you have a file do below:-

perl -wlane ' /\b\d{2,}\-\d{5,}\b/ and $phone=$&; @nums=(split /-/,$phone) ;
if (int($nums[0])==0 || int($nums[1]==0)){print "Phone number not valid at line  number $."} ; '  infile.txt

;);):D:cool::cool:

Hi Thanks for the reply...

But i am not able to put this solution to my code .(may be i am trying wrong)
so can u please help me to fit solution in my code ?

i am pasting my code part here.. can you please help me to add the second validation inside my current code.

open(TMPOUTPUT, "<$tmp_output_file") or die "Can't open output";
open(OUTPUT, ">$output_file") or die "Can't open output";

while ($line = <TMPOUTPUT>)
{
  $line =~ s/ +\n//;
  ($cur_phone, $xxxxxx, $xxxxxx) = split(";", $line);
  ($cur_phone_prefix, $cur_phone_postfix) = split("-", $cur_phone);

  $length_prefix = length ($cur_phone_prefix);
  $length_postfix = length ($cur_phone_postfix);
  if ($length_prefix < 2 || $length_postfix < 5 )
  {
    print "Invalid phone number: $cur_phone \n";
  }
  else
  {
    print "Valid phone number: $cur_phone \n";
  }
}
close TMPOUTPUT;
close OUTPUT;

Thanks in advance,
Bala

That's a Perl one-liner. Just execute it on your file as mentioned. The opening, closing etc. of the file is all done for you in a transparent fashion.

tyler_durden

Hi ,

It looks like the command "perl -wlane" is not working inside my programm..
Can anybody help in this ??

Thanks in advance..

Sure, you will need to execute it on your shell prompt for it to work.

tyler_durden