International Mobile Numbers
15 posts by 3 authors in: Forums > CMS Builder
Last Post: January 31, 2011 (RSS)
By Toledoh - December 31, 2010
Is there a way to automatically check / format numbers?
I want members to enter their mobile number, and most people will enter "0411 999 999", and I need it to change to 61411999999, ie to add the international code (61 for Aust), remove the first "0", check that the next number is "4" and then strip the gaps.
Is there a good way of implementing this?
Tim (toledoh.com.au)
Re: [Toledoh] International Mobile Numbers
Sounds like a job for preg_replace (and a bit of trial and error).
Do a search here on the forum for preg_replace and you might find the approach that you're looking for.
Hope you'll post your solution here.
Good luck,
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [Toledoh] International Mobile Numbers
By Jason - January 3, 2011
Try this function:
function format_mobile_number($phone){
//strip out spaces
$phone = str_replace(" ","",$phone);
$tmpPhone = str_split($phone);
if(@$tmpPhone[1]==4){
$tmpPhone[0]=61;
$phone=implode($tmpPhone);
}
else{
//error
return false;
}
return $phone;
}
You can use it like this:
$phoneNumber= format_mobile_number("0411 999 999");
If the 2nd digit isn't a 4 it returns false, otherwise it returns the formatted string.
hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] International Mobile Numbers
By Toledoh - January 5, 2011
Tim (toledoh.com.au)
Re: [Jason] International Mobile Numbers
By Toledoh - January 25, 2011
I'm not too sure how to use this.
I've got the sign-up page, and the edit profile page where there's the text field "phone".
Should I put some form of error checking on those pages, or within the section editor? Where should I use the code you've supplied?
I really want people to be able to put in their mobile number in the format they recognise (ie. 0411 422 274, or 0411422274), but then have it stored in the database as 61411422274 so it's usable with the SMS Plugin.
Appreciate your help!
Tim (toledoh.com.au)
Re: [Toledoh] International Mobile Numbers
By Jason - January 26, 2011
This function will returned a formatted mobile phone number. The idea is that this formats a phone number entered, so you would use this after a form has been submitted, before you add it to your INSERT/UPDATE statement.
A good place to do this would be where you're doing your form error checking:
EXAMPLE:
$phoneNumber= format_mobile_number(@_REQUEST['phone_number']);
if(!$phoneNumber){**ERROR**}
Provided there's no error, you would then use $phoneNumber in your query.
Hope this helps clarify.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] International Mobile Numbers
By Toledoh - January 27, 2011
I've added
// Mobile Number Format
function format_mobile_number($mobile){
//strip out spaces
$mobile = str_replace(" ","",$mobile);
$tmpPhone = str_split($mobile);
if(@$tmpPhone[1]==4){
$tmpPhone[0]=61;
$phone=implode($tmpPhone);
}
else{
//error
return false;
}
return $mobile;
}
just before the // update user
I changed the $phone to $mobile to be consistent with the field name I'm using.
I can now edit my profile, and put the mobile number in as "123" and it's happily accepted - but it shouldn't be.
I've attached the profile.php page...
Tim (toledoh.com.au)
Re: [Toledoh] International Mobile Numbers
By Jason - January 28, 2011
First, you need to call the function to format the mobile number. You can then check to make sure what was entered was valid:
I've highlighted the changes in red:
// check for duplicate usernames and emails
if (@$_REQUEST['username'] != $CURRENT_USER['username']) {
$count = mysql_select_count_from('accounts', "`username` = '".mysql_escape(@$_REQUEST['username'])."'");
if ($count > 0 && @$_REQUEST['username']) { $errorsAndAlerts .= "That username is already in use, please choose another!<br/>\n"; }
}
if (@$_REQUEST['email'] != $CURRENT_USER['email']) {
$count = mysql_select_count_from('accounts', "'".mysql_escape($_REQUEST['email'])."' IN (email, username)");
if ($count > 0) { $errorsAndAlerts .= "That email is already in use, please choose another!<br/>\n"; }
}
//format mobile number.
$mobile = format_mobile_number(@$_REQUEST['mobile']);
if(!$mobile){ $errorsAndAlerts .= "You must enter a valid mobile number! <br/>\n";}
After that, you just need to use the $mobile variable in your UPDATE query:
mobile = '".mysql_escape($mobile)."',
Hope this helps.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] International Mobile Numbers
By Toledoh - January 28, 2011
That almost there now - thanks!
It's accepting 0411422274, and 0411 422 274. No biggy, but is there a way to also check it has a total of 10 digits? (not including spaces)
That will now get the phone number in, in the format that everyone understands.
However, to use the mobile number with the SMS Plugin, I now need the numbers to be trimmed of the first "0" then add "61" infront .
Maybe when we use
mobile = '".mysql_escape($mobile)."',
we can also have something like.$internationalMobile = $mobil (trim 0 AND +61);
internationalMobile = '".mysql_escape($mobile)."',
Your thoughts?
Tim (toledoh.com.au)
Re: [Toledoh] International Mobile Numbers
By Jason - January 28, 2011
The function I provided should put a 61 at the front of the number if the second is 4. So 0411422274 would become 61411422274. Is that not what's happening?
Could you re-attach the .php file you're using so I can see what's happening. Also, please let me know what values the function is returning for you.
Thanks
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/