Last active
July 2, 2020 13:34
-
-
Save herewithme/c0cda1e1ce738c8825c2a7dcb13b3fa0 to your computer and use it in GitHub Desktop.
An addon for WPForms plugin, allow to force and valid french format phone number for the field with the class: "wp-forms-check-phone-number"
This file contains 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: WPforms - French Phone Validation | |
* Plugin URI: https://beapi.fr | |
* Description: Add JS and PHP check for validate french number, supported formats : +33123456789 and 0123456789 | |
* Version: 0.2 | |
* Author: BE API | |
* Author URI: https://beapi.fr | |
*/ | |
add_action( 'wp_footer', function () { | |
?> | |
<script> | |
(function($) { | |
var WPforms_FrenchPhone_Validation = { | |
init: function() { | |
$(document).on('wpformsReady', WPforms_FrenchPhone_Validation.customRules) | |
}, | |
customRules: function() { | |
if (typeof $.fn.validate !== 'undefined') { | |
$.validator.addMethod( | |
'regex', | |
function(value, element, regexp) { | |
if (regexp.constructor != RegExp) | |
regexp = new RegExp(regexp) | |
else if (regexp.global) | |
regexp.lastIndex = 0 | |
return this.optional(element) || regexp.test(value) | |
}, 'Votre numéro de téléphone ne semble pas valide' | |
) | |
} | |
if ($('.wp-forms-check-phone-number input').length) { | |
$('.wp-forms-check-phone-number input').rules('add', { | |
'regex': /^(\+33|0)[0-9]{9}$/ | |
}) | |
} | |
}, | |
} | |
WPforms_FrenchPhone_Validation.init() | |
})(jQuery); | |
</script> | |
<?php | |
} ); | |
/** | |
* Add PHP validation for custom rule | |
* | |
* @param int $field_id | |
* @param array $field_submit | |
* @param array $form_data | |
* | |
* @since 1.0.0 | |
*/ | |
function wpforms_process_validate_text_french_phone( $field_id, $field_value, $form_data ) { | |
$form_id = $form_data['id']; | |
$fields = $form_data['fields']; | |
if ( ! empty( $fields[ $field_id ]['css'] ) ) { | |
$classes = explode( ' ', $fields[ $field_id ]['css'] ); | |
if ( in_array( 'wp-forms-check-phone-number', $classes ) ) { | |
if ( ! preg_match( "/^(\+33|0)[0-9]{9}$/i", $field_value ) ) { | |
wpforms()->process->errors[ $form_id ][ $field_id ] = 'Votre numéro de téléphone ne semble pas valide'; | |
return; | |
} | |
} | |
} | |
} | |
add_action( 'wpforms_process_validate_text', 'wpforms_process_validate_text_french_phone', 10, 3 ); |
Hello,
Your code works ! thank you !
How to force the selection of the country "France" ? Because on mobile, this is not selected by default.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
If I create two text fields with same validation css class wp-forms-check-phone-number (eg: "your phone" and "phone of your wife"), there is no validation for the second phone number.)
I suppose it is due to WpForms main code and not yours but it is ennoying.
Best Regards