Last active
April 7, 2026 09:02
-
-
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 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: 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I faced a similar problem and ended up building a free WordPress plugin that handles this without touching any code. It supports minimum/maximum length, blocks repetitive digits like 1111111, has a blacklist, country code filtering, IP throttling, and validation logs. Works with all WPForms phone fields automatically. Might be useful if you need a more universal solution:
https://wordpress.org/plugins/phone-validator-for-wpforms/
https://github.com/Ilyntiy/phone-validator-for-wpforms