Perl script :- Phone number validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script :- Phone number validation
# 1  
Old 04-01-2010
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
# 2  
Old 04-01-2010
use below:-


Code:
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:-


Code:
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


SmilieSmilieSmilieSmilieSmilie
# 3  
Old 04-01-2010
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.
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

Last edited by Scott; 04-01-2010 at 01:29 PM.. Reason: Code tags, please...
# 4  
Old 04-01-2010
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
# 5  
Old 04-01-2010
Hi ,

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

Thanks in advance..
# 6  
Old 04-01-2010
Quote:
Originally Posted by subin_bala
...
It looks like the command "perl -wlane" is not working inside my programm..
Can anybody help in this ??
...
Sure, you will need to execute it on your shell prompt for it to work.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Web Development

Auto phone number search

Hi. I want to search 10 phone numbers automatically in Facebook and store the result in some format. Can anyone help me with the script. I am using kali Linux. (2 Replies)
Discussion started by: looksthatmatter
2 Replies

2. Shell Programming and Scripting

Editing phone number with multiple delimiters

Hello all I have a data base of information that is formatted like so: JSD4863 XXX-XX-XXXX DOE, JOHN C JR-II BISS CPSC BS INFO TECH 412/779-9445 I need the last four digits of the phone number. However, many lines contain 'garbage data' that I'm not interested in. So i used a 'for loop'... (7 Replies)
Discussion started by: smartSometimes
7 Replies

3. Shell Programming and Scripting

Number of arguments to array - Perl Script

I have the following proc. proc get_add {arg1 arg2 arg3 arg4 arg 5 .. ... arg N } { } i need to count the number of arguments and also i need those arguments stored in an array. please help out ---------- Post updated at 06:33 PM ---------- Previous update was at 05:30 PM ---------- ... (1 Reply)
Discussion started by: Syed Imran
1 Replies

4. Shell Programming and Scripting

Getting phone number, its message and assigning them into 2 variables then screen output.

Hi Everyone, I have a flatfile "inbox.txt" which contains some information: Location 0, folder "Inbox", SIM memory, Inbox folder SMS message SMSC number : "+24800000023" Sent : Sat 04 Aug 2012 09:01:00 PM +0700 Coding : Default GSM alphabet... (5 Replies)
Discussion started by: testcase
5 Replies

5. Shell Programming and Scripting

count number of entries perl program or Unix script

Hi I have a file with number of entries name 1 123 name 1 345 name 1 65346 name2 3243 name2 24234 name 2 234234 so on ......... how to count total number of entries for name 1 and name2...and so on Please guide. (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

perl: How to improve with minimal validation of its input??

The Code: #!/usr/bin/perl use strict; use warnings; print "Please enter numbers, separated by commas: "; my $data=<STDIN>; chomp $data; my @dataset=split(/,/, $data); my $sum = 0; foreach my $num (@dataset) { $sum += $num; } my $total_nums = scalar(@dataset); my $mean =... (1 Reply)
Discussion started by: 300zxmuro
1 Replies

7. Shell Programming and Scripting

perl linux file name validation

Hi Everyone, #!/usr/bin/perl $a = ".a!"; if ($a =~ s///g) { print "invalid file name\n"; } else { print "valid file name\n"; } but the output is: Invalid range "_-." in regex; marked by <-- HERE in m// at ./a.pl line 5. the linux file name should be A-Z, a-z,... (8 Replies)
Discussion started by: jimmy_y
8 Replies

8. Shell Programming and Scripting

form validation with perl

Hey guys, I'm just messing around with a perl webpage. The idea is to make a simple validation form that will later insert a record into my DVD database. it's all very basic at the moment, and I worked up my script from the form validation example I found on this website:... (5 Replies)
Discussion started by: LNC
5 Replies

9. Shell Programming and Scripting

Date & NUmber Validation Query

Hi Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format. I used following regular expression to validate integer, which is not working in the default shell: nodigits="$(echo $testvalue | sed 's/]//g')" ... (4 Replies)
Discussion started by: alok_jax
4 Replies
Login or Register to Ask a Question