Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Telephone Number : 1 big field, into 3 smaller fields ?

Telephone Number : 1 big field, into 3 smaller fields ?
Thread Tools
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 21, 2003, 08:15 PM
 
I'm currently using this code in my PHP script to process my html form ..

// check phone for validity

if (($PHONE_NO) || ($phone_no)) {

$phone_no = trim($phone_no);

if ($PHONE_NO)

$phone_no = trim($PHONE_NO);

if (!ereg("[long thing...]", $phone_no)) {

print_error("your <b>phone number</b> is invalid");

}

}

Now I want to change the form, so there's three fields [area code, prefix, suffix] for the telephone number. The problem is that, when the script emails me the phone number, I want it to be written as 10 digits, not as three separate values.

I don't want ...

phone1 : ###
phone2 : ###
phone3 : ####

I DO want ....

phone_no : ##########

I've tried using this code .... but it didn't work ....

if (trim($phone1) and trim($phone2) and trim($phone3) || $phone1=trim($PHONE1) and $phone2=trim($PHONE2) and $phone3=trim($PHONE3)) {

if (!ereg("^[0-9]{3}$",$phone1) || !ereg("^[0-9]{3}$",$phone2) || !ereg("^[0-9]{4}$",$phone3)) {

print_error("your <b>phone number</b> is invalid");

} else {

$phone_no = $phone1."-".$phone2."-".$phone3;

}

}
And I'm also trying to make sure the person enters in a NUMBER and not alpha-numeric ...

any help would be GREATLY appreciated!!! thanks!!
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 21, 2003, 10:03 PM
 
use an array instead...

$phone[0] = area code
$phone[1] = prefix
$phone[2] = suffix

to validate, keep what you have now, just run the parts through one at a time...

for (i = 0; i < count($phone); ++i)
{
if (!ereg("[long thing...]", $phone[i]))
{
print_error("your phone number is invalid");
}
}

then after the parts have been validated, join them back into a string before emailing...

$phoneNumber = join("-", $phone);
(Last edited by mzllr; Oct 21, 2003 at 10:23 PM. )
     
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
Oct 21, 2003, 11:23 PM
 
sadly you can't use an array that effectively (last one contains 4 digits). this is probably what you'll want:

[php]
$phone[0]=$HTTP_GET_VARS['areacode'];
$phone[1]=$HTTP_GET_VARS['first3'];
$phone[2]=$HTTP_GET_VARS['last4'];

$valid_a=ereg("^[0-9]{3}$",$phone[0])? true:false;
$valid_b=ereg("^[0-9]{3}$",$phone[1])? true:false;
$valid_c=ereg("^[0-9]{4}$",$phone[2])? true:false;

if(!($valid_a && $valid_b && $valid_c)) {
// i like 'die' more then print_error
die("bad phone number");
}

$fullphone=join("-",$phone);
[/php]

the only thing that might be hard to understand in that code is the ereg lines, that is a one line version of an if statement, in the form of:
(variable) = (boolean expression)? (if true) : (if false);

--will
     
badtz  (op)
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 22, 2003, 12:40 PM
 
On the above code ...

even entering all numerics correctly, it still comes up saying "bad phone number"?

     
badtz  (op)
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 22, 2003, 01:55 PM
 
if ((!ereg("^[0-9]{3}$",$phone1) || !ereg("^[0-9]{3}$",$phone2) || !ereg("^[0-9]{4}$",$phone3))) {
print_error("your <b>phone number</b> is invalid");
} else {
$phone_no =$phone1 ."-" .$phone2 ."-" .$phone3 ;
}
So far, by using this code everything is validating perfectly! [3 digits, 3 digits, 4 digits and ONLY numerics] .....

the part that's NOT working is concatenating the three fields [phone1,phone2,phone3] into ONE long field of 10 digits [no hyphens]....

if anyone could help on this last problem, that'd be MUCH appreciated!!!


thanks!
     
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 22, 2003, 03:31 PM
 
instead of...

[php]
$phone_no =$phone1 ."-" .$phone2 ."-" .$phone3 ;
[/php]

maybe try..

[php]
$phone_elements = array("$phone1", "$phone2", "$phone3");

$phone_no = join(" - ", $phone_elements);
[/php]
     
badtz  (op)
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 22, 2003, 03:37 PM
 
It still sends the email as three variables [$phone1, $phone2, $phone3] ....

is there a way to send the $phone_no variable via the email, but suppress emailing the other three variables????

http://www.agregardie.com/form/php4.txt

^ that's the code of the complete script. This phone_no field starts around line 620.

????
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Oct 22, 2003, 04:44 PM
 
First change your HTML input boxes to read:

Code:
< input type="text" name="email[phone1]" value="<?php print $_POST['email']['phone1'] ?>" />
(...and do the same for phone2 and phone 3)

Then change your validation code to read:

Code:
extract($_POST['email']); if ((!ereg("^[0-9]{3}$",$phone1) || !ereg("^[0-9]{3}$",$phone2) || !ereg("^[0-9]{4}$",$phone3))) { print_error("your phone number is invalid"); } else { unset($_POST['email']['phone1']); unset($_POST['email']['phone2']); unset($_POST['email']['phone3']); $_POST['email']['phone'] =$phone1 ."-" .$phone2 ."-" .$phone3 ; }
Finally edit the email script where it says:

Code:
$content = parse_form($HTTP_POST_VARS);
to read

Code:
$content = parse_form($_POST['email']);
Essentially, this means the code will only check through the array 'email' in your POST variables (which makes error checking somewhat easier).

The 'unset' option removes the individual phone number components from the array, then we re-insert the joined phone string back into the array as 'phone'.

If you have other information that needs to be sent in the email along with this phone number, simply give them the form name of 'name="email[variable]"'.

Arrays and forms are a very cool part of PHP - check the manual for other tricks you can do (like automatically walking through arrays and validating code, extracting lists of variables, etc.)
Computer thez nohhh...
     
badtz  (op)
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 22, 2003, 05:05 PM
 
I did everything stated above .....

whenever the form is submitted, it says "the phone number is invalid" ... no matter if there's a telephone number entered or not .....



It does look like it should work though, but I can't figure out what the error might be?

?? thanks for your help so far tho
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Oct 22, 2003, 06:54 PM
 
Originally posted by badtz:
I did everything stated above .....
Could you post the HTML code of your page as well as the PHP? What version are you running?
Computer thez nohhh...
     
badtz  (op)
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status: Offline
Reply With Quote
Oct 23, 2003, 04:25 PM
 
The script can be viewed at ..

http://www.agregardie.com/form/php4.txt

the verison of PHP is 4.3.2


would it be easier to have the variable always show up as 10 digits with no parentheses and no hyphens.

Currently I have one long field, and when someone types in there number, it can be in any format ...

ie. (213)123-1234
213-123-1234
2131231234
etc. etc. etc.

Is there anyway to do it to where it always emails me 10 digits straight?

The code I currently use is this :

// check phone for validity



if_(($PHONE_NO)_||_($phone_no))_{



$phone_no_=_trim($phone_no);



if_($PHONE_NO)



$phone_no_=_trim($PHONE_NO);



if_(!ereg("((^[0-9]{3}-[0-9]{3}-[0-9]{4}$)|(^[0-9]{3}-[0-9]{7}$)|(^(([0-9]{3}))[0-9]{3}-[0-9]{4}$)|(^(([0-9]{3}))_[0-9]{3}-[0-9]{4}$)|(^(([0-9]{3}))[0-9]{7}$)|(^(([0-9]{3}))_[0-9]{7}$)|(^[0-9]{10}$))",_$phone_no))_{



print_error("your <b>phone number</b> is invalid");



}



}
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Oct 23, 2003, 06:31 PM
 
Originally posted by badtz:
The script can be viewed at ..
I meant your actual HTML code where your form information is kept. No matter, though.

If you just wanted to parse a form field for correct information (removing erroneous parentheses, brackets, dashes and spaces) then you could try:

Code:
$phone_no = trim($_POST['phone_no']); $phone_no = preg_replace("/[\-\(\)\s]/","",$phone_no); if (!preg_match("/^\d{10}$/",$phone_no)) { print_error("your phone number is invalid"); } ...mailing code goes here...
This simply removes any of the offending characters from your input string, checks that the result is a number of 10 digits and then you do the rest.
Computer thez nohhh...
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:18 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2