Phone Number as 3 separate fields

Discussion in 'Customization & add-ons' started by fishnyc22, Apr 6, 2006.

  1. fishnyc22

    fishnyc22 New Member

    Joined:
    Mar 22, 2006
    Messages:
    29
    Hey Everyone.

    When a user signs up, their Phone Number is very important in my DB, so I'd like to ensure its submitted properly.

    What is the correct way to customize the code so I have three fields on the signup form that I can then validate for correct US lengths [3] [3] [4]. If correct, I'd then concatonate the number into 555-555-5555.

    Also, I'm going to need some additional fields in the member table for some things that the user will not see. Will adding them with phpMyAdmin create an issue later with upgrading. Or will it mess up aMember if I do so?

    Thanks

    Dave
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    amember/site.inc.php

    PHP:
    <?php
    function vsf(&$vars){
       
    $err = array();
       
    $phone preg_replace('/\D+/'''$vars['phone']);
       if (
    strlen($phone) != 10) {
           
    $err[] =  "Phone number must contain 10 digits";
           return 
    $err;
       } 
       
    $vars['phone'] = substr($phone03) . '-' .
                             
    substr($phone33) . '-' 
                             
    substr($phone64);
    }
    setup_plugin_hook('validate_signup_form''vsf');
    ?>
  3. fishnyc22

    fishnyc22 New Member

    Joined:
    Mar 22, 2006
    Messages:
    29
    Thanks Alex.

    I understand what you're suggesting. I was hoping however to split the signup form up into 3 fields so they tab through the 3 fields:
    [ ]-[ ]-[ ]

    Those 3 fields (say 'phone1', 'phone2', and 'phone3') are concatenated to be ###-###-#### and entered into 'phone' in the DB. Doing this with "additional fields" would create 3 fields in the DB. I would not want to do that. Should I just hard code this? If so, where would be the proper place to put this.

    Thanks for the help.
  4. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    If you provide FTP info and aMember CP login info, we will make this change for you. It is too long to explain here.
  5. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    (please contact us via support helpdesk)
  6. MeanderingCode

    MeanderingCode New Member

    Joined:
    Apr 29, 2006
    Messages:
    16
    Error in storage

    Alex -

    When i placed your code in a site.inc file, the result was that the database only stores the first three digits in the phone field.

    Is this due to that field being an int field?
    How can this be fixed? (i like displaying the number broken up...easy for my customer to read)

    Thanks
  7. fishnyc22

    fishnyc22 New Member

    Joined:
    Mar 22, 2006
    Messages:
    29
    Hey,

    I finally got around to resolving this by using one field as alex suggested. It worked perfectly stripping out any unwanted characters and inserting "-" to format the number. It does need to be set to varchar but thats fine.

    Works great. Thanks again alex.
  8. MeanderingCode

    MeanderingCode New Member

    Joined:
    Apr 29, 2006
    Messages:
    16
    Where do you change the phone field to varchar?

    __________________________________________
    Never mind, forgot that phone was a custom field.
  9. fishnyc22

    fishnyc22 New Member

    Joined:
    Mar 22, 2006
    Messages:
    29
    in amember pro, you must have created the field using "add fields" in the admin. you can change the type to varchar there.
  10. lcutting

    lcutting New Member

    Joined:
    Aug 22, 2006
    Messages:
    1
    This would help solve my problem as well but that's file not in my version.
  11. tfrangio

    tfrangio New Member

    Joined:
    Aug 23, 2008
    Messages:
    11
    I have need to support a phone field and a date field on my aMember Signup page. What is the best way to make sure they are validated?

    In this thread it seems like Alex has some custom code that handles the validation?
  12. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Yes above example is valid for phone field validation.
  13. sunseapromos

    sunseapromos Member

    Joined:
    Apr 26, 2010
    Messages:
    72
    I need to add a phone field to the sign up form also.

    I want to be sure I get this straight :eek: so it works :D

    1.) put the above .php code into my site.inc page

    2.) then add a field in the amember admin - I have questions about these settings:
    ==> what is the display type - text or text area?
    ==> input fields - the size of input field has a default of "20" what should be there?
    ==> default value for the field - what should this be?
    ==> validation function - what should this be?

    Thanks! :)
    Claudia
  14. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Display type should be set to text
    Size - leave as is
    default value leave empty
    validation function - required
  15. sunseapromos

    sunseapromos Member

    Joined:
    Apr 26, 2010
    Messages:
    72
    Thanx! Alexander,

    I did the phone field without the three sections and this worked seemlessly first time round! (for a change :D)

    Claudia
  16. kanjigirl

    kanjigirl New Member

    Joined:
    May 23, 2011
    Messages:
    10
    Hi Alex,

    I added your PHP to my site.inc.php file, changing the variable name to phone_office to match the one I created in the admin custom fields.

    Code:
    function vsf(&$vars){
       $err = array();
       $phone = preg_replace('/\D+/', '', $vars['phone_office']);
       if (strlen($phone) != 10) {
           $err[] =  "Phone number must contain 10 digits";
           return $err;
       } 
       $vars['phone'] = substr($phone, 0, 3) . '-' .
                             substr($phone, 3, 3) . '-' . 
                             substr($phone, 6, 4);
    }
    setup_plugin_hook('validate_signup_form', 'vsf');
    
    It's not working though, the field is required but it's not validating. Can you help? Here's the form:

    http://www.cvsinc.net/amember/signup.php

    I also have a phone_cell field that I need to do validation on too.
  17. kanjigirl

    kanjigirl New Member

    Joined:
    May 23, 2011
    Messages:
    10
    My apologies - it actually is working fine.

    Code:
    /* Validation for phone_office field*/
    function vsf(&$vars){
       $err = array();
       $phone = preg_replace('/\D+/', '', $vars['phone_office']);
       if (strlen($phone) != 10) {
           $err[] =  "Office number must contain 10 digits";
           return $err;
       } 
       $vars['phone_office'] = substr($phone, 0, 3) . '-' .
                             substr($phone, 3, 3) . '-' . 
                             substr($phone, 6, 4);
    }
    setup_plugin_hook('validate_signup_form', 'vsf');
    
    /* Validation for phone_cell field*/
    function vsfcell(&$vars){
       $err = array();
       $phone_cell = preg_replace('/\D+/', '', $vars['phone_cell']);
       if (strlen($phone_cell) != 10) {
           $err[] =  "Cell phone number must contain 10 digits";
           return $err;
       } 
       $vars['phone_cell'] = substr($phone_cell, 0, 3) . '-' .
                             substr($phone_cell, 3, 3) . '-' . 
                             substr($phone_cell, 6, 4);
    }
    setup_plugin_hook('validate_signup_form', 'vsfcell');

Share This Page