Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // CPF MASK
- // Add this class to the CPF field on the form: cpf
- add_action('wp_footer', 'wpmudev_custom_cpf_mask', 9999);
- function wpmudev_custom_cpf_mask()
- {
- global $post;
- if (is_a($post, 'WP_Post') && !has_shortcode($post->post_content, 'forminator_form')) {
- return;
- }
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- function handleCpfChange(event) {
- // Get only the numbers from the data input
- let data = event.target.value.replace(/\D/g, "");
- // Checking data length to define if it is cpf or cnpj
- if (data.length <= 11) {
- // It's cpf
- let cpf = "";
- let parts = Math.ceil(data.length / 3);
- for (let i = 0; i < parts; i++) {
- if (i === 3) {
- cpf += `-${data.substr(i * 3)}`;
- break;
- }
- cpf += `${i !== 0 ? "." : ""}${data.substr(i * 3, 3)}`;
- }
- // Pass formatting for the data
- data = cpf;
- }
- // Update state
- setcpfCnpjValue(data);
- };
- function setcpfCnpjValue(value) {
- // Set the value to the input
- cpf_field.val(value);
- }
- const cpf_field = $(".cpf input");
- cpf_field.bind('focusout keyup ', function(event) {
- handleCpfChange(event);
- });
- });
- </script>
- <?php
- }
- ###################################
- // CNJP MASK
- // Add this class to the CNJP field on the form: cnjp
- add_action('wp_footer', 'wpmudev_custom_cnpj_mask', 9999);
- function wpmudev_custom_cnpj_mask()
- {
- global $post;
- if (is_a($post, 'WP_Post') && !has_shortcode($post->post_content, 'forminator_form')) {
- return;
- }
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- function handleCnpjChange(event) {
- // Get only the numbers from the data input
- let data = event.target.value.replace(/\D/g, "");
- // Checking data length to define if it is cpf or cnpj
- if (data.length > 11) {
- // It's cnpj
- let cnpj = `${data.substr(0, 2)}.${data.substr(2,3)}.${data.substr(5,3)}/`;
- if (data.length > 12)
- cnpj += `${data.substr(8, 4)}-${data.substr(12, 2)}`;
- else
- cnpj += data.substr(8);
- // Pass formatting for the data
- data = cnpj;
- }
- // Update state
- setcpfCnpjValue(data);
- };
- function setcpfCnpjValue(value) {
- // Set the value to the input
- cpf_field.val(value);
- }
- const cpf_field = $(".cnpj input");
- cpf_field.bind('focusout keyup ', function(event) {
- handleCnpjChange(event);
- });
- });
- </script>
- <?php
- }
- #################################
- // CNJP validation
- // replace number 123 with an ID of your form
- // replace input-1 with your CNJP field name
- // set your custom message in $error_msg
- add_filter( 'forminator_custom_form_submit_errors', 'cnjp_form_validation', 99, 3 );
- function cnjp_form_validation( $submit_errors, $form_id, $field_data_array ) {
- $my_forms = array( 123 ); // IDs of forms to check; if more than one, separate by comma
- $cnjp_field = 'input-1'; // CNJP field
- $error_msg = 'Invalid CNJP!'; // custom error message
- if ( in_array( $form_id, $my_forms ) ) {
- $cnjp = '';
- foreach( $field_data_array as $arr ) {
- if ( $arr['name'] == $cnjp_field ) $cnjp = $arr['value'];
- }
- // validation
- if ( !cnjp_validate( $cnjp ) ) {
- $submit_errors[][$mail_field] = $error_msg;
- }
- }
- return $submit_errors;
- }
- function cnjp_validate( $cnjp ) {
- $b = array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
- $c = preg_replace('/[^\d]/', '', $cnpj);
- if(strlen($c) !== 14)
- return false;
- if(preg_match('/0{14}/', $c))
- return false;
- for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
- if($c[12] != (((($n %= 11)) < 2) ? 0 : 11 - $n))
- return false;
- for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
- if($c[13] != (((($n %= 11)) < 2) ? 0 : 11 - $n))
- return false;
- return true;
- }
- ###################################
- // CPF Validation
- // replace 123 with your form ID
- // replace input-1 with your CPF field
- // set your error message in $error_msg
- add_filter( 'forminator_custom_form_submit_errors', 'cpf_form_validation', 99, 3 );
- function cpf_form_validation( $submit_errors, $form_id, $field_data_array ) {
- $my_forms = array( 123 ); // IDs of forms to check; if more than one, separate by comma
- $cpf_field = 'input-1'; // CNJP field
- $error_msg = 'Invalid CPF!'; // custom error message
- if ( in_array( $form_id, $my_forms ) ) {
- $cpf = '';
- foreach( $field_data_array as $arr ) {
- if ( $arr['name'] == $cpf_field ) $cpf = $arr['value'];
- }
- // validation
- if ( !cpf_validate( $cpf ) ) {
- $submit_errors[][$mail_field] = $error_msg;
- }
- }
- return $submit_errors;
- }
- function cpf_validate( $cpf ) {
- // Extracts only the numbers
- $cpf = preg_replace( '/[^0-9]/is', '', $cpf );
- // Checks if all digits were entered correctly
- if (strlen($cpf) != 11) {
- return false;
- }
- // Checks whether a sequence of repeated digits was entered. Ex: 111.111.111-11
- if (preg_match('/(\d)\1{10}/', $cpf)) {
- return false;
- }
- // Perform the calculation to validate the CPF
- for ($t = 9; $t < 11; $t++) {
- for ($d = 0, $c = 0; $c < $t; $c++) {
- $d += $cpf[$c] * (($t + 1) - $c);
- }
- $d = ((10 * $d) % 11) % 10;
- if ($cpf[$c] != $d) {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement