• dbabcock

    (@danielbabcock)


    Hello all,

    I’m using Brilliant Web-to-Lead for Salesforce and need to validate phone numbers to legitimate formats to keep our data clean.
    I’m trying to usesfwp2l_validate_field but all I’ve done so far is break the form. Anyone have a solution for validating phone numbers with this plugin?

    This is the HTML for the input area.
    <input type="text" placeholder="" value="" id="sf_phone" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required required" name="phone">

    This is what I’ve scratched together so far as a solution attempt after reading through some documentation and a lot of stackoverflow.

    
    add_filter('sfwp2l_validate_field', 'validatePhoneNumber', 10, 4);
    
    function validatePhoneNumber($phone)
    {
    	$dom = new DOMDocument();
    
    	$dom->loadHTML($html);
    
    	$xpath = new DOMXPath($dom);
    	// $phone = $xpath->query('//input[id="sf_phone"]');
    	  $phone = $xpath->evaluate('string(//input[@id="sf_phone"])')
    
    	// $phone = '(123) 458-1542';
    
    	if (preg_match('~^\(*\+*[1-9]{0,3}\)*-*[1-9]{0,3}[-. /]*\(*[2-9]\d{2}\)*[-. /]*\d{3}[-. /]*\d{4} *e*x*t*\.* *\d{0,4}$~', $phone)) {
    		echo "Matched: $phone";
    	}	
    }
    • This topic was modified 4 years, 9 months ago by dbabcock.
Viewing 1 replies (of 1 total)
  • Plugin Author brilliantplugins

    (@brilliantplugins)

    Parsing the DOM is not required ??

    The second argument to add_filter needs to match the number of arguments your function accepts — that’s likely what broke your site (4 != 1).

    Try this instead:

    
    add_filter('sfwp2l_validate_field', 'validatePhoneNumber', 10, 4);
    
    function validatePhoneNumber($error, $name, $val, $field){
    
        if( 'phone' == $name ){
    
            if ( !preg_match('~^\(*\+*[1-9]{0,3}\)*-*[1-9]{0,3}[-. /]*\(*[2-9]\d{2}\)*[-. /]*\d{3}[-. /]*\d{4} *e*x*t*\.* *\d{0,4}$~', $val)){
                $error['valid'] = false;
                $error['message'] = 'Please enter a valid phone number in the format XXX-XXX-XXXX ext 123.';
            }
    
        }
    
        return $error;
    
    }
    
Viewing 1 replies (of 1 total)
  • The topic ‘Phone Validation?’ is closed to new replies.