79180644

Date: 2024-11-12 10:14:48
Score: 1
Natty:
Report link
//To validate an Australian phone number in Contact Form 7:

//Add Phone Field in CF7:

<div class="form-fields-wrapper"> 
    <label>PHONE NUMBER <span>*</span></label>
    [text* phone-number class:phone-number id:phone-number minlength:10 maxlength:10]
</div>

//Add jQuery Script in Footer:

<script>
jQuery(function($){
    var phoneNumberField = $('#phone-number');
    phoneNumberField.on('input', function() {
        var currentValue = $(this).val().replace(/[^0-9]/g, '');
        phoneNumberField.next('.phone-error').remove();

        if (currentValue && !currentValue.startsWith('04')) {
            phoneNumberField.after('<div class="phone-error" style="color: red;">Please enter a valid Australian number.</div>');
        } else {
            $(this).val(currentValue);
        }
    });
});
</script>

//Explanation: This script restricts input to numbers only and shows an error if the number doesn’t start with "04".

https://darshanvachhani12.wordpress.com/

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Darshan Vachhani