Strip special characters and spaces from phone submission

Is there a way to strip out everything aside from numbers, from a phone field value that’s submitted?

i.e. they enter (123) 456-7890
and the value is recorded as 1234567890

That stripping/replacing of special characters could happen instantaneously on input or submission so long as the final value passed doesn’t contain them.

I’ve searched through past topics but could only find ones which set custom validation to have an error message pop up requiring the person to remove the characters. This is more work for the submitter and would reduce conversion rates so I don’t want to do that.

Thanks for any ideas/assistance.


Hi Peter. You can modify the value before storing that in the entry using the gform_save_field_value filter.

This code will do it:

// Change 540 to your form id number, and 3 to your field id.
add_filter( 'gform_save_field_value_540_3', 'only_numbers_please', 10, 4 );
function only_numbers_please( $value, $lead, $field, $form ) {
	GFCommon::log_debug( __METHOD__ . '(): Original value => ' . $value );
	$value = preg_replace("/[^0-9]/", "", $value );
	GFCommon::log_debug( __METHOD__ . '(): Modified value => ' . $value );
	return $value;

This is PHP code that can be added to your theme functions.php file, or you can use a plugin such as this one to keep the code separate from the theme: Code Snippets – WordPress plugin |

Please see this article for additional information on placing PHP snippets:

I am not sure what happens if you use this with a phone field, because that data is stored with the formatting (for example: (708) 555-1212). If you change the data to store it in another format, I’m not sure what will break. You can definitely do this with a single line text field.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.