Created
August 2, 2020 09:45
-
-
Save wpmudev-sls/ef388f11a3ef68ef0e968be061d46f1e to your computer and use it in GitHub Desktop.
[Forminator] Custom number format for Calculation field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [Forminator] Custom number format for Calculation field. | |
| * Description: [Forminator] Custom number format for Calculation field. | |
| * Jira: SLS-470 | |
| * Author: Thobk @ WPMUDEV | |
| * Author URI: https://premium.wpmudev.org | |
| * License: GPLv2 or later | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| return; | |
| } | |
| /** | |
| * Note, to make this work: | |
| * 1. Enable load form using Ajax on Behaviour tab | |
| * 2. Add class name wpmudev-calculation into calculation field (Calculation field settings -> Styling tab) | |
| */ | |
| add_action( 'wp_footer', 'wpmudev_forminator_custom_number_format_calculation', 21 ); | |
| function wpmudev_forminator_custom_number_format_calculation(){ | |
| ?> | |
| <script> | |
| (function($){ | |
| /** | |
| * To use addSeparatorsNF, you need to pass it the following arguments: | |
| * @param {number} nStr The number to be formatted, as a string or number. | |
| * No validation is done, so don't input a formatted number. | |
| * If inD is something other than a period, then nStr must be passed in as a string. | |
| * @param {string} inD The decimal character for the input, such as '.' for the number 100.2 | |
| * @param {string} outD The decimal character for the output, such as ',' for the number 100,2 | |
| * @param {string} sep The separator character for the output, such as ',' for the number 1,000.2 | |
| * @source http://www.mredkj.com/javascript/nfbasic.html | |
| */ | |
| function addSeparatorsNF(nStr, inD, outD, sep) | |
| { | |
| nStr += ''; | |
| var dpos = nStr.indexOf(inD); | |
| var nStrEnd = ''; | |
| if (dpos != -1) { | |
| nStrEnd = outD + nStr.substring(dpos + 1, nStr.length); | |
| nStr = nStr.substring(0, dpos); | |
| } | |
| var rgx = /(\d+)(\d{3})/; | |
| while (rgx.test(nStr)) { | |
| nStr = nStr.replace(rgx, '$1' + sep + '$2'); | |
| } | |
| return nStr + nStrEnd; | |
| } | |
| if (typeof ($.fn.forminatorLoader) === 'undefined') { | |
| // nothing to do here | |
| } else { | |
| // support ajax form | |
| var _separated_character = ' ';// Enter separated character. | |
| $(document).on('after.load.forminator', function(e, _form_id){ | |
| $('#forminator-module-'+ _form_id).find('.wpmudev-calculation .forminator-calculation').on('change', function(){ | |
| let _this = $(this), | |
| _next = _this.next(".forminator-input"); | |
| if( ! _next.length ){ | |
| _this.after('<input type="text" class="forminator-input" value="'+ addSeparatorsNF( _this.val(), ',', ',', _separated_character) +'" disabled="true" />'); | |
| _this.hide(); | |
| }else{ | |
| _next.val( addSeparatorsNF( _this.val(), ',', ',', _separated_character) ); | |
| } | |
| }); | |
| }); | |
| } | |
| })(window.jQuery); | |
| </script> | |
| <?php | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment