Posts: 16
Threads: 8
Joined: Jul 2009
Reputation:
0
Oct 21st, 2009, 5:13 PM
(This post was last modified: Apr 4th, 2010, 12:53 PM by Ben.)
Hi there,
I'm hoping this is easy to accomplish. Unfortunately, my hands are still dirty with tones of PHP beginner lessons and I still need some basic help sorting things out.
I have a web form that collects personal data such as names and phone numbers.
I would like to be able to reformat the data entered in the phone number field when it's not formated the standard way.
Here's how it should be entered: (555) 555-5555. If the user fails to enter it that way, the Submission Pre-Parser module should correct it.
I don't want to overload the page with javascripts/jquery to create an automatic mask. I can't use CSS styles either, because I'm using them for something else.
Any help will be appreciated.
Posts: 2,456
Threads: 39
Joined: Dec 2008
Reputation:
6
Hey Invectus,
Sure, no problem! Users can enter the characters in any old way, so you need to think up all the different scenarios.
I'd approach it like this:
1. Strip out all non-numeric characters
2. Depending on the length of the string, do different things. If it's exactly 10 characters, you know the user conscientiously entered the area code + full phone number. If it's 11, they entered the long distance "1" as well. If it's only 7 chars, they omitted the area code.
If it's something else...??? Who knows! Users will ALWAYS find a way to screw it up, so we need to take care of those cases too.
Here's some code you can enter into your Submission Pre-Parser rule:
PHP Code: $phone = $_POST["phone"]; $phone = preg_replace("/\D/", "", $phone); $num_chars = strlen($phone);
$formatted_phone_num = ""; if ($num_chars == 10) { $formatted_phone_num = "(" . substr($phone, 0, 3) . ") " . substr($phone, 3, 3) . "-" . substr($phone, 6, 4); } else if ($num_chars == 11) { $formatted_phone_num = substr($phone, 0, 1) . " (" . substr($phone, 1, 3) . ") " . substr($phone, 4, 3) . "-" . substr($phone, 7, 4); } else if ($num_chars == 7) { $formatted_phone_num = substr($phone, 0, 3) . "-" . substr($phone, 3, 4); } else { $formatted_phone_num = $phone; }
$_POST["phone"] = $formatted_phone_num;
That will take care of those scenarios I mentioned, and if the phone number is a non-standard length, it'll just leave it alone. You WILL need to update the $_POST["phone"] key with whatever your phone number field is called.
Hope this helps!
- Ben
Posts: 16
Threads: 8
Joined: Jul 2009
Reputation:
0
(Oct 31st, 2009, 10:51 AM)Ben Wrote: Hey Invectus,
Sure, no problem! Users can enter the characters in any old way, so you need to think up all the different scenarios.
I'd approach it like this:
1. Strip out all non-numeric characters
2. Depending on the length of the string, do different things. If it's exactly 10 characters, you know the user conscientiously entered the area code + full phone number. If it's 11, they entered the long distance "1" as well. If it's only 7 chars, they omitted the area code.
If it's something else...??? Who knows! Users will ALWAYS find a way to screw it up, so we need to take care of those cases too.
Here's some code you can enter into your Submission Pre-Parser rule:
PHP Code: $phone = $_POST["phone"]; $phone = preg_replace("/\D/", "", $phone); $num_chars = strlen($phone);
$formatted_phone_num = ""; if ($num_chars == 10) { $formatted_phone_num = "(" . substr($phone, 0, 3) . ") " . substr($phone, 3, 3) . "-" . substr($phone, 6, 4); } else if ($num_chars == 11) { $formatted_phone_num = substr($phone, 0, 1) . " (" . substr($phone, 1, 3) . ") " . substr($phone, 4, 3) . "-" . substr($phone, 7, 4); } else if ($num_chars == 7) { $formatted_phone_num = substr($phone, 0, 3) . "-" . substr($phone, 3, 4); } else { $formatted_phone_num = $phone; }
$_POST["phone"] = $formatted_phone_num;
That will take care of those scenarios I mentioned, and if the phone number is a non-standard length, it'll just leave it alone. You WILL need to update the $_POST["phone"] key with whatever your phone number field is called.
Hope this helps!
- Ben
As always Ben, very helpful advice and I like the logic behind it.
It worked like a charm.
Thanks a bunch buddy.
Cheers,
Invectus
|