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.