【问题标题】:Wordpress contact form 7 indian 10 digit phone number validationWordpress 联系表格 7 印度 10 位电话号码验证
【发布时间】:2019-11-15 12:27:10
【问题描述】:

我想在没有插件的情况下验证“联系表格 7”印度 10 位电话号码验证。

下面是我在插件中使用的模式:

因此,在 Contact Form 7 formatting.php 模块中,我进行了这样的更改,但仍然无法验证:

function wpcf7_is_tel( $tel ) {
    $result = preg_match( '/^[+]?[0-10() -]*$/', $tel );
    return apply_filters( 'wpcf7_is_tel', $result, $tel );
}

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    所以,在 Contact Form 7formatting.php 模块中,

    function wpcf7_is_tel( $tel ) {
        $result = preg_match( '/^[+]?[0-9() -]*$/', $tel );
        return apply_filters( 'wpcf7_is_tel', $result, $tel );
    }
    

    替换为添加模式行 /^([1]-)?[0-9]{10}$/i

    function wpcf7_is_tel( $tel ) {
            $result = preg_match( '/^([1]-)?[0-9]{10}$/i', $tel );  // added line
            return apply_filters( 'wpcf7_is_tel', $result, $tel );
        }
    

    注意:最多接受10位数字

    【讨论】:

    • 除了这个答案,我建议在使用它之前测试正则表达式。这可以在这里完成,例如regex101.com/r/ueX5OD/2
    【解决方案2】:

    这应该可以解决问题

    function wpcf7_is_tel( $tel ) {
            $result = preg_match( '^\d{10}$/i', $tel );  // added line
            return apply_filters( 'wpcf7_is_tel', $result, $tel );
        }
    

    【讨论】:

    • 感谢您的回复@lvollmer。上述模式不起作用。但是这种模式对我有用----> /^([1]-)?[0-9]{10}$/i
    猜你喜欢
    • 2016-05-27
    • 2021-08-06
    • 2013-06-06
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多