Problem: I would like to apply masking to a CRED form field using jQuery.mask(), but it doesn't seem to do anything.
Solution:
- Masking is not native to jQuery, and it requires a separate plugin file. You must enqueue the jQuery plugin file "jquery.maskedinput.js" in your child theme, or use another plugin to enqueue it. Here's the plugin on github: https://github.com/digitalBush/jquery.maskedinput/
- You must use the 'jQuery' namespace instead of '$' in any JavaScript code. Replace '$' with 'jQuery' everywhere in your JavaScript.
- You must wrap your JavaScript code in an event listener callback so it is executed after jQuery and CRED have both been initialized.
- The attribute "id" is not a valid attribute on a cred_field shortcode, so the ID "tel" selector is not going to work. You can use a class instead, something like 'js-cred-tel', and then modify your jQuery code to use that selector.
Example:
jQuery(window).bind('load', function(){ jQuery(".js-cred-tel").mask("(99) 9999?9-9999"); jQuery(".js-cred-tel").on("blur", function() { var last = jQuery(this).val().substr( jQuery(this).val().indexOf("-") + 1 ); if( last.length == 3 ) { var move = jQuery(this).val().substr( jQuery(this).val().indexOf("-") - 1, 1 ); var lastfour = move + last; var first = jQuery(this).val().substr( 0, 9 ); jQuery(this).val( first + '-' + lastfour ); } }); });
Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 6 years, 10 months ago.
Assisted by: Christian Cox.