FORUMS


The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pre-Parser module: Format phone number field
#1
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.
Reply
#2
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($phone03) . ") " substr($phone33) . "-" substr($phone64);
} else if (
$num_chars == 11) {
    
$formatted_phone_num substr($phone01) . " (" substr($phone13) . ") " substr($phone43) . "-" substr($phone74);
} else if (
$num_chars == 7) {
    
$formatted_phone_num substr($phone03) . "-" substr($phone34);
} 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
Reply
#3
(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($phone03) . ") " substr($phone33) . "-" substr($phone64);
} else if (
$num_chars == 11) {
    
$formatted_phone_num substr($phone01) . " (" substr($phone13) . ") " substr($phone43) . "-" substr($phone74);
} else if (
$num_chars == 7) {
    
$formatted_phone_num substr($phone03) . "-" substr($phone34);
} 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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)