LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2010, 02:35 PM   #1
Harris Jayaraj
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
Perl : 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 ..

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
 
Old 04-01-2010, 03:28 PM   #2
majorleap
LQ Newbie
 
Registered: Mar 2005
Posts: 3

Rep: Reputation: 1
Use the character class grouping [] combined with the ^ (meaning characters not in this class).

# check #1 - phone number should be at least digits, then a dash, then
# at least 5 digits
# check #2 - first section cannot be all 0's
# check #3 - second section cannot be all 0's
# Quick reg exp refresh
# \d = digit
# \d{n,} = at least n digits
# ^ - start of string
# \ escape a metacharacter (like a dash) to mean its literal value
# $ - end of string
# [] character class (match anything inside of the brackets
# [^] character class negated (match on anything NOT inside the brackets

### if $phone_number is the number you want to check
if (
$phone_number =~ /^\d{2,}\-\d{5,}$/ ### check #1
and $phone_number =~ /^[^0]{2,}\-/ ### check #2
and $phone_number =~ /\-[^0]{5,}$/ ### check #3
) {
print "GOOD: $phone_number\n";
} else {
print "BAD: $phone_number\n";
}

You could also use the !~ operator to do check 2 and 3
e.g. $phone_number !~ /^0+\-/
or
$phone_number !~ /^0{2,}-/
 
Old 04-02-2010, 02:21 AM   #3
Harris Jayaraj
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Hi ,

THanks for the help..
I have tried the code and the third one "# check #3 - second section cannot be all 0's " is not working properly...

Can you please help me to find out what went wrong ?
 
Old 04-02-2010, 05:56 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You may wish to explain what is not working properly?
ie. is it specific data that is not working?
 
Old 04-02-2010, 07:39 AM   #5
Harris Jayaraj
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Hi ,

If ia m giving input phnoe number as 01-00000 , still this code returns GOOD phone number.


Regards,
Harris
 
Old 04-02-2010, 08:01 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well that is curious, because I copy/pasted that code into a script and whenever
either part of the number has a zero in it I am told the number is BAD??
So I would be interested to know if you changed any of it as our results are very different?

However, this seems to work for me:

Code:
if ($phone_number =~ /^\d{2}\-\d{5}$/ ### check #1
and $phone_number !~ /^0{2}\-/ ### check #2
and $phone_number !~ /\-0{5}$/ ### check #3) 
{
print "GOOD: $phone_number\n";
} 
else 
{
print "BAD: $phone_number\n";
}

Last edited by grail; 04-02-2010 at 08:02 AM.
 
Old 04-02-2010, 08:27 AM   #7
majorleap
LQ Newbie
 
Registered: Mar 2005
Posts: 3

Rep: Reputation: 1
Lots of reasons a regular expression match can fail. Off the top of my head, I'd guess you have white space somewhere in your string. Try putting single quotes or some other character around the GOOD/BAD messages. For example:

print "BAD: '$phone_number'\n";

If $phone_number == '01-00000\n' or '01-00000 ' the third check will fail.

For a simple case like this one, print statements are usually enough to figure out what's going wrong. For more complex cases, you can break the 3 checks into individual checks and nest them (e.g. if (check 1) { if (check 2) { if (check 3) { ... which makes it easier to figure out exactly which check is failing.

Finally, I'd suggest learning about perl -d (the perl debugger). Just type perl -d <your perl program + options> and then type h at the prompt. This is an invaluable tool for debugging issues of any complexity.
 
Old 04-02-2010, 08:32 AM   #8
majorleap
LQ Newbie
 
Registered: Mar 2005
Posts: 3

Rep: Reputation: 1
also, all the code here is off the top of my head w/o any testing... so don't assume it just works. I think the idea is good and I tried to write it correctly, but "code that is not tested does not work correctly".
 
Old 04-03-2010, 12:39 PM   #9
Harris Jayaraj
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Hi Grail/Majorleap,

THe code is working fine for me now
For supporting more than 2 charcters in prefix and morethan 5 characters in postfix i added a comma in the first condition...

#!/bin/perl

my $phone_number;

print "enter phone number\n";
chomp ($phone_number = <STDIN>);

if ($phone_number =~ /^\d{2,}\-\d{5,}$/ and $phone_number !~ /^0{2}\-/ and $phone_number !~ /\-0{5}$/)
{
print "GOOD: $phone_number\n";
}
else
{
print "BAD: $phone_number\n";
}

I have tested this and working in all conditions...
Thanks alot for both of yours help and comments.

Ragrds,
Harris
 
Old 04-04-2010, 01:31 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Great that you found a working solution, please don't forget to mark as SOLVED.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Android phone number 2 coming to 3 LXer Syndicated Linux News 0 05-21-2009 10:42 PM
Use Modem to Call Phone Number avatardeviva Linux - Server 2 02-28-2008 08:21 PM
Find out the owner of a phone number? jmmbacon Linux - General 1 07-06-2007 02:43 PM
local virtual phone number nestor_manya General 0 11-17-2006 08:48 AM
kppp, phone number error DeNieD Linux - Software 1 08-17-2003 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration