PluginProbe
Contact Forms by Cimatti / 2.3.5
Contact Forms by Cimatti v2.3.5
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / classes / Validation / Phone.php

Phone.php in Contact Forms by Cimatti 2.3.5, at classes/Validation/Phone.php

100 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Phone Validation for Contact Forms
4 *
5 * Follows ITU-T E.164 standard (7-15 digits) with lenient formatting.
6 * Allows flexible formatting: spaces, dashes, dots, slashes, parentheses, plus sign.
7 * Requires minimum 7 digits for actual phone numbers (most countries have 7+ digit numbers).
8 * Data is stored as plain text - this is purely a frontend/validation enhancement.
9 *
10 * @package Contact Forms
11 * @subpackage Validation
12 * @since 2.0.0-beta.34
13 */
14
15 class AccuaForm_Validation_Phone extends Validation {
16 protected $message = "Error: %element% must contain a valid phone number.";
17
18 /**
19 * Constructor
20 *
21 * @param string $message Custom error message (optional). Use %element% as placeholder for field label.
22 */
23 public function __construct($message = "") {
24 if (!empty($message)) {
25 $this->message = $message;
26 } else {
27 // translators: %element% is the field label.
28 $this->message = __( "Please enter a valid phone number in %element%", 'contact-forms' );
29 }
30 }
31
32 /**
33 * Validate phone number following ITU-T E.164 standard
34 *
35 * E.164 specifies maximum 15 digits (not counting the + sign).
36 * We require a minimum of 7 digits for actual phone numbers.
37 * We allow flexible formatting for user convenience:
38 * - Minimum 7 digits (most countries have 7+ digit numbers)
39 * - Maximum 15 digits (E.164 compliant)
40 * - Allows: digits, spaces, dashes, dots, slashes, parentheses, plus sign
41 * - Plus sign only allowed at the beginning
42 * - Prefix-only values (≤4 digits with +) are treated as empty for optional fields
43 *
44 * @param string $value The phone number to validate
45 * @return bool True if valid or empty (empty handled by Required validation), false otherwise
46 */
47 public function isValid($value) {
48 // Skip validation for empty/null values (handled by Required validation if needed)
49 if ($this->isNotApplicable($value)) {
50 return true;
51 }
52
53 // Trim whitespace
54 $value = trim($value);
55
56 // Empty after trim is valid (Required validation handles mandatory fields)
57 if ($value === '') {
58 return true;
59 }
60
61 // FIRST: Check for invalid characters (only digits, spaces, dashes, dots, slashes, parentheses, plus)
62 if (!preg_match('/^[\d\s\-\.\/\(\)\+]+$/', $value)) {
63 return false;
64 }
65
66 // Plus sign only allowed at the start
67 if (strpos($value, '+') !== false && strpos($value, '+') !== 0) {
68 return false;
69 }
70
71 // Multiple plus signs not allowed
72 if (substr_count($value, '+') > 1) {
73 return false;
74 }
75
76 // Count digits only
77 $digitCount = strlen(preg_replace('/\D/', '', $value));
78
79 // Treat prefix-only values (1-4 digits starting with +) as empty for optional fields
80 // This allows optional phone fields with placeholder prefixes like "+39" to pass validation
81 if (strpos($value, '+') === 0 && $digitCount <= 4) {
82 return true;
83 }
84
85 // Require minimum 5 digits for actual phone numbers
86 // NOTE: Client-side uses libphonenumber which may accept 5-6 digit numbers for some countries
87 // Server must be equal or more lenient to avoid rejecting client-accepted values
88 if ($digitCount < 5) {
89 return false;
90 }
91
92 // E.164 standard: maximum 15 digits
93 if ($digitCount > 15) {
94 return false;
95 }
96
97 return true;
98 }
99 }
100