Reply LinkBack Thread Tools Search this Thread
Old 19-12-2007, 21:31   #1 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
PHP Phone Number Validation

How do you incorporate this
PHP Code:
if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$number)) 
into something like:


PHP Code:
else if(!isEmail($email)) {
        
$errmsg 'Please enter your valid e-mail address';
    } 

is it something like this???
PHP Code:
else if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$number)) {
        
$errmsg 'Please enter your valid phone number'

Is that how you do it? this is to validate a phone number 000-000-0000.
  Reply With Quote
Old 19-12-2007, 21:33   #2 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
ALSO! how do you validate a text field that has to be either a valid phone number or can be left empty.??? soo if the person does not input a phone number in then it will not validate it as being invalid. But if the person does, it has to be a valid phone number !

Last edited by Maciek : 19-12-2007 at 22:21.
  Reply With Quote
Old 19-12-2007, 23:38   #3 (permalink)
.sleep
Senior Member
 
.sleep's Avatar
 
Join Date: Mar 2007
Location: california
Posts: 292
Send a message via AIM to .sleep
from php.net

PHP Code:
<?php
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"$date$regs)) {
    echo 
"$regs[3].$regs[2].$regs[1]";
} else {
    echo 
"Invalid date format: $date";
}
?>

so then your code would be:
PHP Code:
if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$number) || $number == "") {
    echo 
"works";
} else {
    
$errmsg 'Please enter your valid phone number';


i'm just guessing as i'm too lazy to test it but you should be able to get going from that.
  Reply With Quote
Old 20-12-2007, 00:01   #4 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,696
It's worth mentioning, if someone enters a number as 080 234 2938 instead of 080-234-2938, the above code will reject it as invalid.
  Reply With Quote
Old 20-12-2007, 01:10   #5 (permalink)
wheedwacker
competitionmaster 2.0
 
wheedwacker's Avatar
 
Join Date: Oct 2006
Location: USA
Posts: 1,420
you could have something before this that converts all the spaces in $number to dashes to fix what Hunch is talking about
  Reply With Quote
Old 20-12-2007, 10:25   #6 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,340
It'll fail if you use something like our corporate standard - 555.555.5555

Or if you leave out the area code.

Or if you use a non-American phone number.
  Reply With Quote
Old 20-12-2007, 19:10   #7 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
I tested
PHP Code:
if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$number) || $number == "") {
    echo 
"works";
} else {
    
$errmsg 'Please enter your valid phone number';


and it is showing up as a php error!.

Any suggestions?
  Reply With Quote
Old 20-12-2007, 19:52   #8 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,696
Code is error free. You must have a problem elsewhere.

Personally I prefer preg_match() to ereg() but it's not a big issue.
  Reply With Quote
Old 20-12-2007, 20:14   #9 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,630
Whats the error that is showing up?
  Reply With Quote
Old 20-12-2007, 23:04   #10 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
When the whole code above is incorporated, it shows an error. I think its because I us "else if".

PHP Code:
if(trim($fname) == '') {
        
$errmsg 'Please enter your first name';
    } 
    else if(
trim($lname) == '') {
        
$errmsg 'Please enter your last name';
    }
    else if(
trim($saddress) == '') {
        
$errmsg 'Please enter your address';
    } 
    else if(
trim($city) == '') {
        
$errmsg 'Please enter your city';
    } 
    else if(
trim($country) == '') {
        
$errmsg 'Please enter your country';
    } 
    else if(
trim($pcode) == '') {
        
$errmsg 'Please enter your postal code';
    } 
    else if(
trim($email) == '') {
        
$errmsg 'Please enter your e-mail address';
    }
    else if(!
isEmail($email)) {
        
$errmsg 'Please enter your valid e-mail address';
    }
    else if(
trim($htelephone) == '') {
        
$errmsg 'Please enter home telephone';
    }
    if(
preg_match("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$htelephone) || $htelephone == "") { 
    echo 
""
} else {
$errmsg 'Please enter your valid phone number'
    } 
    else if(
trim($ccard) == '') {
        
$errmsg 'Please enter the credit card number';
    }
    else if(
trim($edate) == '') {
        
$errmsg 'Please enter the expiry date of the card you entered';
    }
    else if(
trim($ncard) == '') {
        
$errmsg 'Please enter the name on the card you entered';
    }
    else if(
trim($password) == '') {
        
$errmsg 'please chose a password for your account';
    }
    else if(
trim($confirmpass) == '') {
        
$errmsg 'Please confirm you the password you have entered';
    } 

this has the code in it. What am i doing wrong? all this code is off a turtorial. so im not sure what the problem is.
  Reply With Quote
Old 20-12-2007, 23:06   #11 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
and "else if" does not work in front of the phone validation code. ive tried playing with it with no luck!. any help will be appriciated! =) thanks
  Reply With Quote
Old 20-12-2007, 23:10   #12 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 229
Send a message via AIM to Cborrow
You are most likely getting errors by using else if then going to if and else and back to else if in this section.

PHP Code:
else if(trim($htelephone) == '') {
        
$errmsg 'Please enter home telephone';
    }
    if(
preg_match("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$htelephone) || $htelephone == "") { 
    echo 
""
} else {
$errmsg 'Please enter your valid phone number'
    } 
    else if(
trim($ccard) == '') {
        
$errmsg 'Please enter the credit card number';
    } 

You'll have to change it to something like.

PHP Code:
else if(trim($htelephone) == '') {
    
$errmsg 'Please enter home telephone';
}
else if(!
preg_match("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$htelephone) || $htelephone != "") { 
    
$errmsg 'Please enter your valid phone number';  

else if(
trim($ccard) == '') {
    
$errmsg 'Please enter the credit card number';


If that is not the error that you are getting it will most likley pop up latter.
__________________
Code { for thought }
"My programs never have bugs, they just develop random features."
  Reply With Quote
Old 20-12-2007, 23:18   #13 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
now it is saying that there is a invaild phone number when there is a vaild phonenumber. 123-123-1234
  Reply With Quote
Old 20-12-2007, 23:50   #14 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 229
Send a message via AIM to Cborrow
change this

PHP Code:
else if(!preg_match("^[0-9]{3}-[0-9]{3}-[0-9]{4}$"$htelephone) || $htelephone != "") { 
    
$errmsg 'Please enter your valid phone number';  


to this

PHP Code:
else if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/s"$htelephone)) { 
    
$errmsg 'Please enter your valid phone number';  


Also I added the /s because without using them it was giving me an error and it was the
quickest way to fix the error.
__________________
Code { for thought }
"My programs never have bugs, they just develop random features."
  Reply With Quote
Old 21-12-2007, 00:28   #15 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,696
As cborrow pointed out, the order of a branching if statement goes as follows...

if
else if (as many times as you want)
else

Theoretically, if you want maximum efficiency, especially in an interpreted language, the if(...) should be the most likely condition so that the test is satisfied and the code immediately branches. The else at the end is a catch all in case none of the other conditions prove to be true.
  Reply With Quote
Old 21-12-2007, 00:46   #16 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,630
Thats a great tip Hunch. Thanks! I've been doing it the opposite way without realizing. I'm going to have to switch that up.
  Reply With Quote
Old 21-12-2007, 01:17   #17 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
soo when you have a whole set of validations, you should order it if, else if... else if.. and then lastly the last one as else?
  Reply With Quote
Old 21-12-2007, 01:43   #18 (permalink)
Maciek
I like Iced Tea
 
Maciek's Avatar
 
Join Date: Nov 2007
Location: Kitchener.Ontario.Canada
Posts: 93
Send a message via MSN to Maciek
Thanks Cborrow!!! i really appreciate your you help! it all works now. Also someone mentioned that if someone doesnt put 123-123-1231 but puts 123 123 1231 without the "-" then it would just add the "-" ... could someone many get me a link to a turtorial that could help me on this? thanks for everything guys
  Reply With Quote
Old 21-12-2007, 01:51   #19 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,696
Quote:
Originally Posted by Maciek
soo when you have a whole set of validations, you should order it if, else if... else if.. and then lastly the last one as else?

Absolutely - think about it in plain English. For example, choosing where to eat.

If you want to eat Pasta, go to Luigi's Restaurant.
Else, if you want to eat Noodles, go to the Panda Garden.
Else, if you want to have Sushi, go to Benihana.
Else, go to McDonalds.

The final else is the 'last resort' if none of the other options are right.
  Reply With Quote
Old 21-12-2007, 01:56   #20 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,696
Quote:
Originally Posted by Maciek
Thanks Cborrow!!! i really appreciate your you help! it all works now. Also someone mentioned that if someone doesnt put 123-123-1231 but puts 123 123 1231 without the "-" then it would just add the "-" ... could someone many get me a link to a turtorial that could help me on this? thanks for everything guys

I don't have a tutorial to link you to, but this should be all you need to know:

str_replace
("-", " ", $htelephone); // replaces a dash with a space in the string $htelephone

str_replace(find_this, replace_with_this, string_to_search);

If you want to use regular expressions, use preg_replace() instead.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8