Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Created November 14, 2013 16:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardW8k/7470240 to your computer and use it in GitHub Desktop.
Save richardW8k/7470240 to your computer and use it in GitHub Desktop.
add an input mask to an input in a Gravity Forms list field column
// enable input mask for form #95 list field #3 columns 1 and 3
add_filter('gform_register_init_scripts_95', 'enable_list_input_mask');
function enable_list_input_mask($form) {
$field_id = "3"; //set field id here
$col_id = array('1','3'); //set column id here
$mask = "9999"; //define mask here, examples at http://www.gravityhelp.com/documentation/page/Input_Mask
//that's it nothing more to configure
$c_sel = array();
foreach($col_id as $c) {
$c_sel[] = ".gfield_list_{$field_id}_cell{$c} input";
}
$c_sels = json_encode($c_sel);
$im_script = "jQuery(sel.join()).mask('{$mask}');";
$script = "var sel = {$c_sels}; {$im_script} jQuery('#field_{$form["id"]}_{$field_id}').on('click', '.add_list_item', function(){{$im_script}});";
GFFormDisplay::add_init_script($form['id'], 'list_input_mask', GFFormDisplay::ON_PAGE_RENDER, $script);
return $form;
}
add_action( 'gform_enqueue_scripts_95', 'list_masked_input_script', 10, 2 );
function list_masked_input_script( $form ) {
wp_enqueue_script( 'gform_masked_input', array( 'jquery' ), false, true );
}
@jemoreto
Copy link

jemoreto commented Apr 3, 2020

Thanks for this!

@Phukit
Copy link

Phukit commented Dec 15, 2020

Hi,

I was wondering if you could post the code to add input mask to a particular field.

I am using the address field and want to add a mask to the zip/postal code field but there is no option there.

I have used this but doesn't seem to work. From the GF website

add_filter( 'gform_input_masks', 'add_mask' );
function add_mask( $masks ) {
$masks['Address Postal Code'] = 'A9A 9A9';
return $masks;
}

Any help would be appreciated.
Thanks.

@brettg88
Copy link

Hello, I'm in the same boat as @ByteOutfitters. I have two list item fields and tried the same solution he did by separating out the two filters, but the input mask only applies to the first filter. Was there ever a solution for this?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment