• linzgreeny

    (@linzgreeny)


    Hello-
    I am using the Contact Form 7 plugin on 2 pages of my site:

    https://www.revenueapex.com/apply-today/
    https://www.revenueapex.com/contact/

    I would like to implement a phone number validation that checks to make sure the phone number entered is in either one of these formats:

    ##########
    ###-###-####
    (###)###-####

    How would I accomplish this? I know it requires adding some lines or programming to the complicated code base of the plugin. I have no idea where to start!

    Thanks for any help that is offered.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey Linzgreeny,
    I was wondering if you figured it out – the phone number validation.
    I am also looking for a solution

    Thanks,

    Dan

    I have not seen any numerical validation … which seems strange … will keep searching!

    Although I haven’t tried it.

    Couldn’t you use three text field’s in the contactform 7 layout and try that?

    And then restrict the contact form fields to 3 numbers, 3 numbers, and 4 number?

    And then output the results with the above style?

    That might be one quick solution.

    RD

    i have solved this by means of this codes:

    first, i went to the plugin folder and edited, contact-form-7/modules/text.php
    and created a new function (please excuse my mistakes in naming conventions).

    1. i inserted this code:
    wpcf7_add_shortcode( ‘digit’, ‘wpcf7_text_shortcode_handler’, true );
    wpcf7_add_shortcode( ‘digit*’, ‘wpcf7_text_shortcode_handler’, true );

    after this:
    wpcf7_add_shortcode( ’email*’, ‘wpcf7_text_shortcode_handler’, true );

    2. i inserted this code:
    if ( ‘digit’ == $type || ‘digit*’ == $type )
    $class_att .= ‘ wpcf7-validates-as-digit’;

    after this:
    if ( ‘text*’ == $type || ’email*’ == $type )
    $class_att .= ‘ wpcf7-validates-as-required’;

    3. i inserted this code:
    add_filter( ‘wpcf7_validate_email*’, ‘wpcf7_text_validation_filter’, 10, 2 );
    add_filter( ‘wpcf7_validate_digit*’, ‘wpcf7_text_validation_filter’, 10, 2 );

    after this:
    add_filter( ‘wpcf7_validate_email’, ‘wpcf7_text_validation_filter’, 10, 2 );

    4. i inserted this code:
    if ( ‘digit’ == $type || ‘digit*’ == $type ) {
    if ( ” == $_POST[$name] ) {
    $result[‘valid’] = false;
    $result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_required’ );
    }
    elseif ( ” != $_POST[$name] && ! is_telnum( $_POST[$name] ) ) {
    $result[‘valid’] = false;
    $result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_digit’ );
    }
    }

    after this if code:
    if ( ’email’ == $type || ’email*’ == $type ) {
    if ( ’email*’ == $type && ” == $_POST[$name] ) {
    $result[‘valid’] = false;
    $result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_required’ );
    } elseif ( ” != $_POST[$name] && ! is_email( $_POST[$name] ) ) {
    $result[‘valid’] = false;
    $result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_email’ );
    }
    }

    second, i went to contact-form-7/includes/functions.php to add the error message for the telephone number validation.

    1. i inserted this code:
    ‘invalid_digit’ => array(
    ‘description’ => __( “There is a field that sender is needed to fill in with numbers”, ‘wpcf7’ ),
    ‘default’ => __( ‘Please fill the required field with numbers.’, ‘wpcf7’ )
    )

    after this code:
    ‘invalid_required’ => array(
    ‘description’ => __( “There is a field that sender is needed to fill in”, ‘wpcf7’ ),
    ‘default’ => __( ‘Please fill the required field.’, ‘wpcf7’ )
    ),

    third, i went to the formatting file located at wp-includes/formatting.php and created a new function

    function is_telnum($telnum)
    {
    $regexp = ‘/^[0-9\+\-]{7,}$/’;

    if(preg_match($regexp, $telnum))
    return true;
    else
    return false;
    }

    NOTE:
    in my code, i created a function wherein the input text for the telephone number will accept minimum of 7 characters (this only includes numbers and characters + and -)

    example of valid inputs:
    +1234567
    01234567
    02-1234567

    Now to use it in the contact form 7, please use this tag just
    Your Phone</td><td>[digit* text-417]

    Last, i created this a required field hehe, haven’t work it as a not required field yet. sorry.

    Anyone tried this? For some reason this code breaking my site.

    I tried this solution and it works perfectly
    thanks cutescar2nis

    Many thanks cutescar2nis.

    I’d make one small clarification/correction in Step 1, Part 2.

    You say that you inserted this code:

    'invalid_digit' => array(
    'description' => __( "There is a field that sender is needed to fill in with numbers", 'wpcf7' ),
    'default' => __( 'Please fill the required field with numbers.', 'wpcf7' )
    )

    after this code:

    'invalid_required' => array(
    'description' => __( "There is a field that sender is needed to fill in", 'wpcf7' ),
    'default' => __( 'Please fill the required field.', 'wpcf7' )
    ),

    It is important to note, that the comma (,) after the last parenthesis in the existing code is necessary and must be added. Without it your code may not work.

    In short, make sure line 38 in functions.php is ),

    I also changed the regular expression to accept parenthesis and whitespaces so I can accept numbers in the format +64 123 456, (03) 456 7890. My is_telnum function now looks like this:

    function is_telnum($telnum)
    {
    	$regexp = '/^[0-9\+\-\)\(\d\s]{7,}$/';
    
    	if(preg_match($regexp, $telnum))
    		return true;
    	else
    		return false;
    }

    Lastly, please note that you may want to make a backup of the ContactForm7 plugin, as this modification will likely be overwritten when you do a one-click upgrade of the plugin. Let’s hope the maker of ContactForm7 adds this feature.

    Frank Schrijvers

    (@frankschrijvers)

    Hi cutescar2nis or jeffballweg,

    i’ve tried your solution, but without success. When i puxh the send button it’s is loading, nothing happens. Maybe you vould send me your code?

    Thanks a lot!

    Frank

    Thanks for the code cutescar2nis, it was really helpfull. ??

    @frankschrijvers i had the same problem and get it working like this, hope that it work for you too:

    if ( ‘digit’ == $type || ‘digit*’ == $type ) {
    if ( ” == $_POST[$name] ) {
    $result[‘valid’] = false;
    //$result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_required’ );
    $result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
    } elseif ( ” != $_POST[$name] && ! is_telnum( $_POST[$name] ) ) {
    $result[‘valid’] = false;
    //$result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_digit’ );
    $result[‘reason’][$name] = wpcf7_get_message( ‘invalid_digit’ );
    }
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Plugin: Contact Form 7] phone number validation’ is closed to new replies.