PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Phone Number Validation

  1. Hi,

    I've seen around that people are having an issue trying to validate phone numbers, so i came up with a backwards compatible solution for validating phone numbers.

    ------------------------------------
    plugins/gravityforms/forms_model.php

    What To Do:
    Add this function below get_input_masks
    in RGFormsModel class. It's
    located around line 1463.
    ------------------------------------

    public static function get_phone_formats() {
    
        $formats = array(
        	'standard' => array(
            	'regex' => '/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/',
            	'name' 	=> 'Standard',
            	'mask'	=> '(###)### - ####'
            )
        );
    
        return apply_filters('gform_phone_formats', $formats);
    }

    ------------------------------------
    plugins/gravityforms/form_detail.php

    What To Do:
    Replace the current selectbox named
    field_phone_format to render a box
    based on get_phone_formats. It's
    located around line 1572.
    ------------------------------------

    <select id="field_phone_format" onchange="SetFieldPhoneFormat(jQuery(this).val());">
    	<option value="international"><?php _e("International", "gravityforms"); ?></option>
    	<?php
    	$formats = RGFormsModel::get_phone_formats();
        foreach($formats as $format_key => $format_details){
        	if((!isset($format_details['regex']) || empty($format_details['regex'])) || ((!isset($format_details['mask']) || empty($format_details['mask'])) && (!isset($format_details['name']) || empty($format_details['name'])))) continue;
        	$name  = (isset($format_details['name']) && !empty($format_details['name'])) ? ((isset($format_details['mask']) && !empty($format_details['mask'])) ? $format_details['name'] .': '. $format_details['mask'] : $format_details['name']) : $format_details['mask'] ;
            ?>
            <option value="<?php echo $format_key; ?>"><?php echo $name; ?></option>
            <?php
        }
        ?>
    </select>

    ------------------------------------
    plugins/gravityforms/form_display.php

    What To Do:
    Replace the current validation method
    used to validate phone numbers. It's
    located around line 1173.
    ------------------------------------

    $formats 	= RGFormsModel::get_phone_formats();
    $regex 		= $formats[$field["phoneFormat"]]['regex'];
    
    if(!empty($value) && !preg_match($regex, $value)){
        $field["failed_validation"] = true;
        if(!empty($field["errorMessage"]))
            $field["validation_message"] = $field["errorMessage"];
    }

    ------------------------------------
    themes/yourtheme/functions.php

    What To Do:
    You need to add this to your
    functions.php file to add your own
    custom phoneformats.
    ------------------------------------

    function add_phone_formats($formats) {
        $formats["danishphone"] = array(
        	'regex' => '/[0-9]{8}/',
        	'name' 	=> 'Danske Telefonnumre',
        	'mask'	=> '########'
        );
        return $formats;
    }
    add_filter("gform_phone_formats", "add_phone_formats");

    ------------------------------------
    Whats left?
    ------------------------------------

    Now you just have to find your phone
    field, select the correct phone format
    you want to validate. And thats it.

    Please note that the default validation
    message is "hardcoded" in gravity forms
    so to display a validation error upon
    form submit, you need to add whatever
    message to "Validation Message" in
    advanced settings.

    Posted 12 years ago on Monday November 12, 2012 | Permalink
  2. A preview of the result:
    Result preview, with danish phonenumber

    Posted 12 years ago on Monday November 12, 2012 | Permalink
  3. Thank you for that code. However, we don't recommend making changes to the plugin files as those changes will be lost between versions when you upgrade the plugin.

    Posted 12 years ago on Tuesday November 13, 2012 | Permalink
  4. I know it's not that smart, but if a phonenumber is required to have a specific format, for further process of the form, it's quite a vital feature.

    I see you dit not implement the code above, although it wouldn't have done anything but improved your plugin (and made some peoples lives a bit easier, including mine ;-))

    This leads me to the question of what it takes to get something implemented into the core of gravity forms, since providing you with the code, and not just a simple, "I need this feature" request?

    Kind regards.

    Posted 11 years ago on Saturday December 8, 2012 | Permalink
  5. I will bring this topic to the attention of the development team.

    Posted 11 years ago on Monday December 10, 2012 | Permalink
  6. I've heard back from the lead developer. We provide the hook to change the phone format so that we do not have to add phone formats for every country to the code. You seem to have used the hooks well and properly, and your code will serve as a template for others who would like to do the same thing. We will keep your suggestion in mind. Thank you.

    Posted 11 years ago on Tuesday December 11, 2012 | Permalink

This topic has been resolved and has been closed to new replies.