Advertisement
adczk

Forminator - CPF/CNJP mask + validate

Feb 12th, 2024
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.09 KB | None | 0 0
  1. <?php
  2.  
  3. // CPF MASK
  4. // Add this class to the CPF field on the form: cpf
  5. add_action('wp_footer', 'wpmudev_custom_cpf_mask', 9999);
  6. function wpmudev_custom_cpf_mask()
  7. {
  8.     global $post;
  9.     if (is_a($post, 'WP_Post') && !has_shortcode($post->post_content, 'forminator_form')) {
  10.         return;
  11.     }
  12. ?>
  13.     <script type="text/javascript">
  14.         jQuery(document).ready(function($) {
  15.  
  16.             function handleCpfChange(event) {
  17.                 // Get only the numbers from the data input
  18.                 let data = event.target.value.replace(/\D/g, "");
  19.                 // Checking data length to define if it is cpf or cnpj
  20.                 if (data.length <= 11) {
  21.                     // It's cpf
  22.                     let cpf = "";
  23.                     let parts = Math.ceil(data.length / 3);
  24.                     for (let i = 0; i < parts; i++) {
  25.                         if (i === 3) {
  26.                             cpf += `-${data.substr(i * 3)}`;
  27.  
  28.                             break;
  29.                         }
  30.                 cpf += `${i !== 0 ? "." : ""}${data.substr(i * 3, 3)}`;
  31.  
  32.                     }
  33.                     // Pass formatting for the data
  34.                     data = cpf;
  35.                 }
  36.                 // Update state
  37.                 setcpfCnpjValue(data);
  38.             };
  39.  
  40.             function setcpfCnpjValue(value) {
  41.                 // Set the value to the input
  42.                 cpf_field.val(value);
  43.             }
  44.  
  45.             const cpf_field = $(".cpf input");
  46.  
  47.             cpf_field.bind('focusout keyup ', function(event) {
  48.                 handleCpfChange(event);
  49.             });
  50.  
  51.         });
  52.     </script>
  53. <?php
  54.  
  55. }
  56.  
  57.  
  58. ###################################
  59.  
  60. // CNJP MASK
  61. // Add this class to the CNJP field on the form: cnjp
  62. add_action('wp_footer', 'wpmudev_custom_cnpj_mask', 9999);
  63. function wpmudev_custom_cnpj_mask()
  64. {
  65.     global $post;
  66.     if (is_a($post, 'WP_Post') && !has_shortcode($post->post_content, 'forminator_form')) {
  67.         return;
  68.     }
  69. ?>
  70.     <script type="text/javascript">
  71.         jQuery(document).ready(function($) {
  72.  
  73.             function handleCnpjChange(event) {
  74.                 // Get only the numbers from the data input
  75.                 let data = event.target.value.replace(/\D/g, "");
  76.                 // Checking data length to define if it is cpf or cnpj
  77.                 if (data.length > 11) {
  78.                     // It's cnpj
  79.                 let cnpj = `${data.substr(0, 2)}.${data.substr(2,3)}.${data.substr(5,3)}/`;
  80.        
  81.                     if (data.length > 12)
  82.                 cnpj += `${data.substr(8, 4)}-${data.substr(12, 2)}`;
  83.  
  84.                     else
  85.                         cnpj += data.substr(8);
  86.                     // Pass formatting for the data
  87.                     data = cnpj;
  88.                 }
  89.                 // Update state
  90.                 setcpfCnpjValue(data);
  91.             };
  92.  
  93.             function setcpfCnpjValue(value) {
  94.                 // Set the value to the input
  95.                 cpf_field.val(value);
  96.             }
  97.  
  98.             const cpf_field = $(".cnpj input");
  99.  
  100.             cpf_field.bind('focusout keyup ', function(event) {
  101.                 handleCnpjChange(event);
  102.             });
  103.  
  104.         });
  105.     </script>
  106. <?php
  107.  
  108. }
  109.  
  110.  
  111. #################################
  112.  
  113. // CNJP validation
  114. // replace number 123 with an ID of your form
  115. // replace input-1 with your CNJP field name
  116. // set your custom message in $error_msg
  117.  
  118.  
  119. add_filter( 'forminator_custom_form_submit_errors', 'cnjp_form_validation', 99, 3 );
  120. function cnjp_form_validation( $submit_errors, $form_id, $field_data_array ) {
  121.    
  122.    
  123.     $my_forms = array( 123 ); // IDs of forms to check; if more than one, separate by comma
  124.     $cnjp_field = 'input-1'; // CNJP field
  125.    
  126.     $error_msg = 'Invalid CNJP!'; // custom error message
  127.    
  128.     if ( in_array( $form_id, $my_forms ) ) {
  129.        
  130.         $cnjp = '';
  131.  
  132.         foreach( $field_data_array as $arr ) {
  133.    
  134.             if ( $arr['name'] == $cnjp_field ) $cnjp = $arr['value'];
  135.            
  136.         }
  137.        
  138.         // validation
  139.        
  140.         if ( !cnjp_validate( $cnjp ) ) {
  141.             $submit_errors[][$mail_field] = $error_msg;
  142.         }
  143.        
  144.    
  145.     }
  146.  
  147.     return $submit_errors;
  148. }
  149.  
  150.  
  151. function cnjp_validate( $cnjp ) {
  152.    
  153.    $b = array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
  154.    $c = preg_replace('/[^\d]/', '', $cnpj);
  155.    if(strlen($c) !== 14)
  156.         return false;
  157.  
  158.     if(preg_match('/0{14}/', $c))
  159.         return false;
  160.  
  161.     for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
  162.     if($c[12] != (((($n %= 11)) < 2) ? 0 : 11 - $n))
  163.         return false;
  164.  
  165.     for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
  166.     if($c[13] != (((($n %= 11)) < 2) ? 0 : 11 - $n))
  167.         return false;
  168.  
  169.     return true;
  170. }
  171.  
  172.  
  173. ###################################
  174.  
  175. // CPF Validation
  176. // replace 123 with your form ID
  177. // replace input-1 with your CPF field
  178. // set your error message in $error_msg
  179.  
  180. add_filter( 'forminator_custom_form_submit_errors', 'cpf_form_validation', 99, 3 );
  181. function cpf_form_validation( $submit_errors, $form_id, $field_data_array ) {
  182.    
  183.    
  184.     $my_forms = array( 123 ); // IDs of forms to check; if more than one, separate by comma
  185.     $cpf_field = 'input-1'; // CNJP field
  186.    
  187.     $error_msg = 'Invalid CPF!'; // custom error message
  188.    
  189.     if ( in_array( $form_id, $my_forms ) ) {
  190.        
  191.         $cpf = '';
  192.  
  193.         foreach( $field_data_array as $arr ) {
  194.    
  195.             if ( $arr['name'] == $cpf_field ) $cpf = $arr['value'];
  196.            
  197.         }
  198.        
  199.         // validation
  200.        
  201.         if ( !cpf_validate( $cpf ) ) {
  202.             $submit_errors[][$mail_field] = $error_msg;
  203.         }
  204.        
  205.    
  206.     }
  207.  
  208.     return $submit_errors;
  209. }
  210.  
  211.  
  212.  
  213. function cpf_validate( $cpf ) {
  214.  
  215.     // Extracts only the numbers
  216.     $cpf = preg_replace( '/[^0-9]/is', '', $cpf );
  217.  
  218.     // Checks if all digits were entered correctly
  219.     if (strlen($cpf) != 11) {
  220.         return false;
  221.     }
  222.  
  223.     // Checks whether a sequence of repeated digits was entered. Ex: 111.111.111-11
  224.     if (preg_match('/(\d)\1{10}/', $cpf)) {
  225.         return false;
  226.     }
  227.  
  228.     // Perform the calculation to validate the CPF
  229.     for ($t = 9; $t < 11; $t++) {
  230.         for ($d = 0, $c = 0; $c < $t; $c++) {
  231.             $d += $cpf[$c] * (($t + 1) - $c);
  232.         }
  233.         $d = ((10 * $d) % 11) % 10;
  234.         if ($cpf[$c] != $d) {
  235.             return false;
  236.         }
  237.     }
  238.    
  239.     return true;
  240. }
  241.  
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement