• I’m trying to require email or phone # on the contact form but everything I’ve tried does not work. Has anyone had luck doing this?

    What’s the best way to go about it? With a new php file in the /modules folder or with some sort of JS?

    It’s something so simple but I cannot find a definitive answer anywhere online.

    https://wordpress.org/plugins/contact-form-7/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Thread Starter jam2z9

    (@jam2z9)

    Takayuki,

    I need to be able to let a user enter either a phone OR an email so that when one is entered the other one is no longer required. One of these must always be there but it doesn’t matter which one they enter.

    I am looking for the same today and have had no luck. I can tell you what has not worked for me (if that helps you not go down paths I have already walked).

    This post does not work with the current version:
    here

    This is the basic principle but I have not got it to work using JS or functions.php:
    here

    I have tried using an event listener (which also does not work):

    Address (street address, city, state, ZIP)
    [text addressroi id:addressroi ]
    Fax Number
    [tel faxroi id:faxroi class:phoneUS]
    Phone Number
    [tel phoneroi id:phoneroi phoneUS class:required]
    
    <script>
    document.getElementById("phoneroi").addEventListener("click", validateAddressFax);
    
    function validateAddressFax(){
    	var value1 = document.getElementById('addressroi').value;
    	var value2 = document.getElementById('faxroi').value;
    	if((value1 = "") && (value2 = "")) {
    	alert("You must enter an mailling address or a fax#");
    	}
    }
    
    </script>

    I have tried putting the script in the header, fotter, in a separate template for the page and in the themes JS and it still does not work.

    The following looked promising but also does not work with the current version. Essentially they added onfocus and onblur and onclick and to the modual text.php (I would love to have these). That looked cool but it needs some updating for the new version of CF7:
    here

    There is a filter for custom validation and an example for email here

    I am a little confused by this. I tried multiple different variations to try to get this code to work in the wpcf7_validate_text filter:

    if ( $contact_form->id == 585 ) { //Number of the page for the form you are sending a PDF on.
    		if (empty( $_POST['addressroi'])&& empty( $_POST['faxroi'] ) ){
    			echo '<script language="javascript">alert("You must enter a mailing address or fax #")</script>';
    		}

    I am not sure how to make it work. I would think that the wpcf7_validate_text filter is what we want but I am confused about how to make it work.

    So that is what I have done.

    This makes the wheel just spin:

    add_filter( 'wpcf7_validate_text', 'custom_text_validation_filter', 10, 2 ); // text field
    function wpcf7_validate_address_fax($result) {
    		if (empty( $_POST['addressroi'])&& empty( $_POST['faxroi'] ) ){
    			$result->invalidate('',"You must enter a mailing address or fax #");
    		}
            return $result;
    }
    Thread Starter jam2z9

    (@jam2z9)

    I’ve tried writing my own filter to check that email address or phone number are not empty but no luck with that either… still tries to require email?

    Ok jam2z9. Here it is!!!

    function faxormail_validation($result,$tag){
    	$addressroi = $tag['addressroi'];//tags are the field names in the CF7 form
    	$faxroi = $tag['faxroi'];
    		if (empty($addressroi)&& empty($faxroi) ){
    			//In the result, the first variable in ()is the tag on the form where you want your
    			//message to appeare in CF7 and the second is the message you want to send as the result
    			$result->invalidate( "addressroi", "// YOU MUST ENTER AN MAILING ADDRESS OR FAX# //" );
    		}
    	return $result;
    }
    add_filter( 'wpcf7_validate_text', 'faxormail_validation', 10, 2 ); // text field
    add_filter( 'wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2 ); //Req. text field.

    It works!

    Well that one almost worked. This one does work:

    function faxormail_validation($result,$tag){
    	if ($_SERVER["REQUEST_METHOD"] == "POST"){
    		if (empty($_POST["addressroi"])&& empty($_POST["faxroi"]) ){
    			//In the result, the first variable in ()is the tag on the form where you want your
    			//message to appeare in CF7 and the second is the message you want to send as the result
    			$result->invalidate( "addressroi", "// YOU MUST ENTER AN MAILING ADDRESS OR FAX# //" );
    		}
    	return $result;
    	}
    }
    add_filter( 'wpcf7_validate_text', 'faxormail_validation', 10, 2 ); // text field
    add_filter( 'wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2 ); //Req. text field.

    Enjoy!

    Thread Starter jam2z9

    (@jam2z9)

    This is the one I wrote a week or two ago but for some reason it isn’t working:

    <?php
    
    function custom_email_validation_filter( $result, $tag ) {
        $tag = new WPCF7_Shortcode( $tag );
     	$name = $tag['name'];
    
        $your_phone = isset( $_POST['your-phone'] ) ? trim( $_POST['your-phone'] ) : '';
        $your_email = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
    
        //If email is not blank and is not a valid email, then return an error for invalid email.
        if($your_email !== '' && !is_email($your_email)) {
    		$result['valid'] = false;
    		$result['reason'][$name] = wpcf7_get_message( 'invalid_email' );
    		return $result;
        }
    
        //If phone and email are blank then return a message saying phone or email are required and return.
        if($your_phone == '' && $your_email == '') {
        	$result['valid'] = false;
    		$result['reason'][$name] = 'Email or Phone Number are required.'
    		return $result;
        }
    
        //These are all the success cases.
        if(is_email($your_email) && $your_phone == '')
        	$result['valid'] = true;
    
        if($your_email == '' && $your_phone !== '')
        	$result['valid'] = true;
    
        if(is_email($your_email) && $your_phone !== '')
        	$result['valid'] = true;
    
        return $result;
    } 
    
    // add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 20, 2 );
    add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 20, 2 );
    
    ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Require Email OR phone #’ is closed to new replies.