Telephone field error message
February 12, 2025 Β· AI Conversations
– **Contact Form 7**
– **Gravity Forms**
– **WPForms**
– **Elementor Forms**
– **Fluent Forms**
Let me know, and I'll guide you on customizing the error message for the telephone field. π
### **Method 1: Using Contact Form 7 Settings**
1. Go to your **WordPress Admin Dashboard**.
2. Navigate to **Contact β Contact Forms**.
3. Click on the form you want to edit.
4. Locate the **telephone field**, which looks like this:
[tel your-phone]
5. Modify it to include a custom error message:
[tel your-phone "Please enter a valid phone number"]
6. Save the form and test it.
—
### **Method 2: Using Custom Validation in `functions.php`**
If you need more control, you can use custom validation in **functions.php**:
1. Open your **theme's functions.php** file.
2. Add the following code:
function custom_tel_validation_filter($result, $tag) {
$tag = new WPCF7_FormTag($tag);
if ($tag->name == 'your-phone') {
$phone = isset($_POST[$tag->name]) ? sanitize_text_field($_POST[$tag->name]) : '';
if (!preg_match('/^+?[0-9s-()]+$/', $phone)) {
$result->invalidate($tag, "Invalid phone number format. Please enter a valid phone number.");
}
}
return $result;
}
add_filter('wpcf7_validate_tel', 'custom_tel_validation_filter', 10, 2);
3. Save the file and test your form.
This will ensure the telephone number follows a valid format and display a **custom error message** when invalid.
Let me know if you need further customization! π
add_filter( 'wpcf7_validate_text*', 'custom_validate_text', 20, 2 );
function custom_validate_tel( $result, $tag ) {
$submission = WPCF7_Submission::get_instance();
if ( ! $submission ) return $result;
$form = $submission->get_contact_form();
// Debug: Log the form title
error_log( 'Form Title: ' . $form->title() );
// Check if the form title matches
if ( $form->title() !== 'Strategisch groeiplan aanvragen.' ) return $result;
// Validate "tel-48"
if ( $tag->name === 'tel-84' && empty( $_POST['tel-84'] ) ) {
error_log( 'Tel field empty – Showing error' );
$result->invalidate( $tag, 'Number and subject are required, please fill it.' );
}
return $result;
}
not working
—
### **Issues & Fixes**
1. **Ensure Correct Field Name**
– You're checking for `'tel-84'`, but verify that your actual field name in Contact Form 7 matches exactly.
– Go to **Contact Form 7 β Your Form** and confirm the exact name of the telephone field.
2. **Correct Hook for Required Field Validation**
– Youβre using `wpcf7_validate_text*`, but for required telephone fields (`tel*`), use `wpcf7_validate_tel*`.
3. **Check if `$_POST['tel-84']` is Set**
– Instead of checking `empty($_POST['tel-84'])`, use `$submission->get_posted_data('tel-84')`.
4. **Debug Logging**
– Add `error_log()` statements to confirm values during form submission.
—
### **Updated Code**
Try replacing your function with this improved version:
add_filter('wpcf7_validate_tel*', 'custom_validate_tel', 20, 2);
function custom_validate_tel($result, $tag) {
$submission = WPCF7_Submission::get_instance();
if (!$submission) return $result;
$form = $submission->get_contact_form();
// Debug: Log the form title
error_log('Form Title: ' . $form->title());
// Check if the form title matches exactly
if ($form->title() !== 'Strategisch groeiplan aanvragen.') {
error_log('Form title does not match.');
return $result;
}
// Get the actual field name
$name = $tag->name;
error_log("Validating field: $name");
// Validate "tel-84"
$phone = $submission->get_posted_data('tel-84');
if (empty($phone)) {
error_log('Phone field is empty - Showing error');
$result->invalidate($tag, 'Number and subject are required, please fill it.');
}
return $result;
}
—
### **Steps to Test**
1. **Enable Debugging in WordPress**
Add this to `wp-config.php` to check for errors:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('log_errors', 1);
@ini_set('display_errors', 0);
– Then check `wp-content/debug.log` after submitting the form.
2. **Confirm Field Name in Contact Form 7**
– Go to your form and ensure the field name is exactly `tel-84`:
[tel* tel-84]
– If it's different, update the PHP code accordingly.
3. **Test Again**
– Submit the form without entering a phone number and check if the error message appears.
Let me know if it still doesn't work! π