WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Contact Form 7] Filter Form Values (1 post)

  1. Jordan Acosta
    Member
    Posted 1 year ago #

    I have a form with a text field for entering phone numbers, and need to strip out any non-numeric characters before using a number in an email address for an SMS gateway:

    1231231234@tmomail.net

    That would send a text message to 123-123-1234, on TMobile. Most SMS gateways won't allow any dashes, so I need to remove them.

    I tried to do this by adding a filter function to 'wpcf7_validate_text'. That looks like it works in the browser (and it can validate the number just fine,) but in the mail logs I see that the email is still being sent with the dashes in the number. I would like to avoid relying on Javascript if possible, because if something goes wrong with the JS then the form would be effectively broken.

    I would be grateful for any help. Here's the filter function I'm currently using:


    function is_phone( $result, $tag ) { // Validate phone numbers
    $type = $tag['type'];
    $name = $tag['name'];

    if( $name == 'phone' ) {
    $stripped = preg_replace( '/\D/', '', $_POST[$name] );
    $_POST[$name] = $stripped;
    if( strlen( $_POST[$name] ) != 10 ) {
    $result['valid'] = false;
    $result['reason'][$name] = $_POST[$name] /*'You must enter a 10 digit U.S. phone number.'*/;
    }
    }
    return $result;
    }
    add_filter( 'wpcf7_validate_text', 'is_phone', 10, 2 );
    add_filter( 'wpcf7_validate_text*', 'is_phone', 10, 2 );

Topic Closed

This topic has been closed to new replies.

About this Topic