Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2012, 01:35 AM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Validating a UK telephone number.

Hi I am in the process of validating my post data and have come to the phone number field.

I'm not quite sure how to approach this.

When I get to that part of my switch, I send it through the intval() function. This seems to take of the 0 on the beginning of the string.

I decided to leave it as a string, and then use settype() just before doing my database stuff.

Here is my code relating to the phone number:
PHP Code:
case "phone":
    
$num_len strlen($value);
    if(!
ctype_digit($value)){
        
$errors[] = "You can only enter digits into the phone field.";
    }
    if(
$num_len != 11){
        
$errors[]  = "Your telephone number must be 11 digits.";
    }
    die();
break; 
I also did a bit of research and found UK landline and mobile numbers contain 11 digits, and the landlines range from 01200 up to 01997 (with the exception of around 50 which are 'not in use' (according to wikipedia)).

Anyone got any suggestions on how to tackle this?

Thank you for any feedback.

Kind regards,

LC.

Last edited by LearningCoder; 11-14-2012 at 01:43 AM..
LearningCoder is offline   Reply With Quote
Old 11-14-2012, 03:00 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
PHP Code:
is_numeric($input
is all you really need for a phone number unless you want to allow international numbers (which I assume you dont)

from my experience people will often add spaces (even when you tell them not to)...e.g. '04033 55555'
so you might be well to $input=str_replace(' ','',$input) , then check for strlen() and is_numeric().
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

Last edited by firepages; 11-14-2012 at 04:10 AM.. Reason: spelin'
firepages is offline   Reply With Quote
Users who have thanked firepages for this post:
LearningCoder (11-14-2012)
Old 11-14-2012, 03:47 AM   PM User | #3
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Thank you very much for the explanation.

What if they start entering characters such as the hyphen (-)?

I need a way so that if they enter anything but numbers it will get rid of it. I know I can add arrays to the first 2 parameters of str_replace() but it could get a little tedious for my liking.

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Old 11-14-2012, 04:27 AM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
good point, ok thinking about it regex is probs the way to go

PHP Code:
$str='0337890- h 345645';
$str preg_replace("/\D/","",$str);  
if(
$str[0]=='0' && strlen($str)==13){
  echo 
'ok';

__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Users who have thanked firepages for this post:
LearningCoder (11-15-2012)
Old 11-14-2012, 12:50 PM   PM User | #5
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
I use a simple str_replace for phone numbers (and credit card numbers). Takes care of spaces, dashes, periods, commas and parenthesis.

PHP Code:
$value_to_clean str_replace(array('-',' ','.','(',')',','),'',$value_to_clean); 
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 11-14-2012, 01:34 PM   PM User | #6
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Quote:
Originally Posted by firepages View Post
good point, ok thinking about it regex is probs the way to go

PHP Code:
$str='0337890- h 345645';
$str preg_replace("/\D/","",$str);  
if(
$str[0]=='0' && strlen($str)==13){
  echo 
'ok';

I'll have a play around with that code and read up on preg_replace(). Regular Expressions are complicated to understand. Tried using an example code someone gave the other day so thought I'd read up on it.

Can't find any good documentation anywhere for detailed explanations etc.

Kind regards,

Lc.
LearningCoder is offline   Reply With Quote
Old 11-14-2012, 01:35 PM   PM User | #7
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Quote:
Originally Posted by djm0219 View Post
I use a simple str_replace for phone numbers (and credit card numbers). Takes care of spaces, dashes, periods, commas and parenthesis.

PHP Code:
$value_to_clean str_replace(array('-',' ','.','(',')',','),'',$value_to_clean); 
What would happen if they enter different characters (say someone is pratting about), does your code compensate for these other characters or malfunction?

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Old 11-14-2012, 08:45 PM   PM User | #8
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
That is used before additional checking to make sure it is just numbers. Those are the most common things used when someone is entering a phone number or credit card number. After that "cleaning" additional checking is done to make sure it's the proper length and only numeric (and, in the case of a credit card number for example, whether or not it passes MOD10 check).
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 11-14-2012, 10:58 PM   PM User | #9
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Ah ok, thank you. I will keep my str_replace() in my code and perform the additional checks.

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Old 11-15-2012, 12:59 AM   PM User | #10
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
Originally Posted by LearningCoder View Post
Regular Expressions are complicated to understand. Tried using an example code someone gave the other day so thought I'd read up on it.....
yup I hate regular expressions as well and the documentation confuses me more often than not , that said the above simply replaces anything that is not a number from 0-9 and is better than string replace (in this case) since it will replace newlines/tabs/+ etc , err well anything that is not a digit
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 11-15-2012, 01:21 AM   PM User | #11
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
That sounds perfect to me. I have just used your code in the site and it's exactly what I want without having many lines of code!

Just wish I understood RegExp!

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Old 11-15-2012, 01:42 AM   PM User | #12
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Is there any particular way to deal with a drop-down list? Just wondering whether I really need a case in my switch for this as the value will always be one of the options.

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Old 11-15-2012, 03:21 AM   PM User | #13
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
sorry, not with you, need more info
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 11-15-2012, 11:37 AM   PM User | #14
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 848
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
I have a list in my form:
Code:
<select name="product_options">
    <option value="default">Please choose a product...</option>
    <option value="benches">Benches</option>
    <option value="bin_stores">Bin Stores</option>
    <option value="gates">Gates</option>
</select>
Just wondering what kind of validation would I have to perform on a drop-down list? Never used one in my forms before so I'm not quite sure if I need to validate anything?

I have 2 text areas so the only validation I perform on these 2 is making sure the user entered 400 characters or less.

I was racking my brains last night thinking of a way that my list needs to be validated and to be honest I cannot think of one. The value which someone selects will always be 'default' or one of the product names i.e 'benches' or 'gates'.

The only thing I can see to validate is whether or not the user selected something, in which case I need to distinguish which product it was.

Sorry if it's still a little vague.

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Old 11-16-2012, 12:55 AM   PM User | #15
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
OK, with you now.
Like you said the user can only select from one of the dropdown boxes so in theory you can only ever get one of your pre-determined values.

That said, if someone was to create a POST request to your webpage via CURL or simply from creating their own form its possible they could add their own options so you need to be aware of that.

Normally you would counter that by ensuring that the POST data came from your server and assuming that ALL user input is potentially evil.
You could check that the incoming product_options is one of your predetermined values or run a filter_var() or more regex to check its a string etc, in your case its probably best to check for one of your predetermined values.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:17 AM.


Advertisement
Log in to turn off these ads.