The following warnings occurred: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.31 (Linux)
|
![]() |
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"]; 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, As always Ben, very helpful advice and I like the logic behind it. It worked like a charm. Thanks a bunch buddy. Cheers, Invectus |