The following warnings occurred:
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.27 (Linux)
File Line Function
/global.php 783 errorHandler->error
/printthread.php 16 require_once
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.27 (Linux)
File Line Function
/global.php 783 errorHandler->error
/printthread.php 16 require_once
Warning [2] Undefined variable $newpmmsg - Line: 40 - File: global.php(841) : eval()'d code PHP 8.1.27 (Linux)
File Line Function
/global.php(841) : eval()'d code 40 errorHandler->error
/global.php 841 eval
/printthread.php 16 require_once
Warning [2] Undefined array key "style" - Line: 909 - File: global.php PHP 8.1.27 (Linux)
File Line Function
/global.php 909 errorHandler->error
/printthread.php 16 require_once
Warning [2] Undefined property: MyLanguage::$lang_select_default - Line: 5024 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 5024 errorHandler->error
/global.php 909 build_theme_select
/printthread.php 16 require_once
Warning [2] Undefined array key "additionalgroups" - Line: 7162 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 7162 errorHandler->error
/inc/functions.php 5044 is_member
/global.php 909 build_theme_select
/printthread.php 16 require_once
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.27 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.27 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.27 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.27 (Linux)
File Line Function
/printthread.php 165 errorHandler->error
Warning [2] Undefined array key "showimages" - Line: 160 - File: printthread.php PHP 8.1.27 (Linux)
File Line Function
/printthread.php 160 errorHandler->error
Warning [2] Undefined array key "showvideos" - Line: 165 - File: printthread.php PHP 8.1.27 (Linux)
File Line Function
/printthread.php 165 errorHandler->error



Form Tools
Pre-Parser module: Format phone number field - Printable Version

+- Form Tools (https://forums.formtools.org)
+-- Forum: Modules / Other (https://forums.formtools.org/forumdisplay.php?fid=8)
+--- Forum: Modules (https://forums.formtools.org/forumdisplay.php?fid=16)
+--- Thread: Pre-Parser module: Format phone number field (/showthread.php?tid=343)



Pre-Parser module: Format phone number field - invectus - Oct 21st, 2009

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.


RE: Format phone number field - Ben - Oct 31st, 2009

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


RE: Format phone number field - invectus - Nov 6th, 2009

(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