 |
 |
Telephone Number : 1 big field, into 3 smaller fields ?
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
On the above code ...
even entering all numerics correctly, it still comes up saying "bad phone number"?

|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
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
|
|
instead of...
[php]
$phone_no =$phone1 ."-" .$phone2 ."-" .$phone3 ;
[/php]
maybe try..
[php]
$phone_elements = array("$phone1", "$phone2", "$phone3");
$phone_no = join(" - ", $phone_elements);
[/php]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
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
|
|
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...
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
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
|
|
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...
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Location: Los Angeles, CA.
Status:
Offline
|
|
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
|
|
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...
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|