Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 2nd, 2009, 01:02 PM
Authorized User
 
Join Date: Oct 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting phone number

Hello,

I am trying to format US phone numbers to look like this (###) ###-#### and the code that I used is:
Code:
function format_phone($phone_1)
{
	$phone = preg_replace("/[^0-9]/", "", $phone_1);

	if(strlen($phone_1) == 7)
		return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone_1);
	elseif(strlen($phone_1) == 10)
		return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone_1);
	else
		return $phone_1;
}
This was inputted into the VirtuemartOrdersReport.php file and it does not seem to work.
 
Old December 29th, 2009, 11:45 AM
Authorized User
 
Join Date: Sep 2003
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Greywacke
Question Formatting phone number

have you tried a more basic approach yet? like the following:

only allow numeric input on this textbox and limit by 10 numbers in form validation:
HTML Code:
<form id="form1" name="form1" method="post" action="handler.php">
     <input id="phone" name="phone" type="text" value="" />
</form>
do the form validation and keystroke limiting something like follows in the external .js file:
Code:
document.form1.onsubmit = function () {
     if (this.value.length == 10) {  // force length of 10.
          return true;
     } else {
          alert("You need a 10 digit phone number including area code!");
          return false;
     }
}
document.formj1.phone.onkeypress = function () {
     if (!e) var e = window.event;
     if ([e.keyCode||e.which]==8||[e.keyCode||e.which]==46) // this is to allow backspace or delete.
          return true;
     if ([e.keyCode||e.which] < 48 || [e.keyCode||e.which] > 57)
          e.preventDefault? e.preventDefault() : e.returnValue = false;
}
handler.php function and usage:
PHP Code:
function format_phone($phone) {
    
$phone = ((strlen($phone)==7)?"012":"").$phone// insert default area code if omitted.
    
$phone "(".substr($phone_103).") ".substr($phone,3,3)."-".substr($phone,6,4);
    return 
$phone;
}
$phone format_phone($_POST("phone")); 
__________________
Sincerely,
Pierre "Greywacke" du Toit
[email protected]
don't worry about my 0 thankyou's either way, i would say thank you should you help, and i will gladly help others when this work rush is over.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
GridView - Phone column formatting jnks2005 ASP.NET 2.0 Basics 1 February 14th, 2007 06:36 PM
Phone # field formatting! Nostromo77 ASP.NET 2.0 Basics 3 December 19th, 2006 02:02 PM
formatting phone numbers xslt_student XSLT 0 August 7th, 2006 04:10 PM
phone number validation debjanib ASP.NET 1.0 and 1.1 Professional 0 June 6th, 2006 12:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.