Search code examples
wordpresscontact-formphone-numbertel

WordPress phone Number Restrict to allow xxx-xxx-xxxx Format


I have Kept the placeholder as xxx-xxx-xxxx

<label> Phone Number </label> [number* shm-mrk-phone minlength:10 maxlength:140 placeholder "]

I changes the tel script in plugin formatting.php

function wpcf7_is_tel( $tel ) {
    $result = preg_match( '/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/', $tel );
    return apply_filters( 'wpcf7_is_tel', $result, $tel );
}

its not allowing xxx-xxx-xxxx( this should allow), its allowing xxxxxxxxxx


Solution

  • It's Very Simple. i used tel instead of number Type to add Mobile Number.

    In Contact Form 7 plugin formatting.php Module,

    i updated the Code of wpcf7_is_tel function with.

    function wpcf7_is_tel( $tel ) {
        $result = preg_match( '/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/', $tel );
        return apply_filters( 'wpcf7_is_tel', $result, $tel );
    }
    

    Works Fine.