【问题标题】:How to validate zipcode in textfield of CF7如何验证 CF7 文本字段中的邮政编码
【发布时间】:2021-03-13 15:39:07
【问题描述】:

我在 CF7 中有一个文本字段,我想验证用户输入是否为 4 位数字和 2 个字符,例如:1234AB(没有空格和大写字母)。

这是我已经拥有的,但它不起作用:

function custom_postcode_validation_filter($result,$tag){

    $name = $tag->name;

    if($name == 'ba-postcode'){

        $bapostcode = $_POST['ba-postcode'];

        if($bapostcode != '') {

            if(!preg_match('/^([0-9]{4})\\([^a-Z]{2})$/', $bapostcode)) {

                $result->invalidate( $tag, "Vul alstublieft een geldige postcode in." );

            }

        }

    }

    return $result;
}   

add_filter( 'wpcf7_validate_text*', 'custom_postcode_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text', 'custom_postcode_validation_filter', 10, 2 );

我认为这可能与这行有关: if(!preg_match('/^([0-9]{4})\\([^a-Z]{2})$/', $bapostcode)) {.

所以我在得到一些帮助后更新了答案。这就是我现在拥有的:

function custom_postcode_validation_filter($result,$tag){

    $name = $tag->name;

    if($name == 'ba-postcode'){

        $bapostcode = $_POST['ba-postcode'];

        if($bapostcode != '') {

            $regex = "/^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i";

if(!preg_match( $regex, $bapostcode)) {
    $result->invalidate( $tag, "Vul alstublieft een geldige postcode in." );
}

    return $result;
}   

add_filter( 'wpcf7_validate_text*', 'custom_postcode_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text', 'custom_postcode_validation_filter', 10, 2 );
    }
 }

它仍然无法正常工作。我错过了什么?

我还希望,如果用户填写的字段没有大写字母,文本字段会自动转换它们。

感谢任何建议!

【问题讨论】:

  • 你的正则表达式是 4 个数字,然后是一个反斜杠,接下来的两个字符不是 a-Z。
  • 如果你想匹配 4 个数字然后 2 个字母,你会想要类似 '/^([0-9]{4}[a-zA-Z]{2}) $/'
  • 我试过了,但不幸的是它没有用。

标签: php wordpress contact-form-7 add-filter


【解决方案1】:

试试这个正则表达式,我在一个荷兰网站中使用过它,它具有相似的结构。

function custom_postcode_validation_filter( $result,$tag ){

    if( 'ba-postcode' == $tag->name ){

        // Get postcode field
        $bapostcode = isset( $_POST['postcode'] ) ? trim( $_POST['postcode'] ) : '';

        // Check postcode is set
        if($bapostcode != '') {

            // match postcodes 1234AB
            $regex = "/^[1-9][0-9]{3}(?!sa|sd|ss)[a-z]{2}$/i";

            // validate postcode
            if( !preg_match( $regex, $bapostcode ) ) {
                $result->invalidate( $tag, "Vul alstublieft een geldige postcode in." );
            }

        }   
    }

    return $result;
}
add_filter( 'wpcf7_validate_text*', 'custom_postcode_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text', 'custom_postcode_validation_filter', 10, 2 );

【讨论】:

  • 谢谢。这是针对荷兰网站的,所以它很相似:-)。我为您的代码更改了这一行 if(!preg_match('/^([0-9]{4})\\([^a-Z]{2})$/', $bapostcode)) {,但不幸的是,这不起作用。它接受我输入的每一个输入。
  • 它在我的本地实例中工作,也许你的页面上有另一个 php 错误?
  • 我将完整的代码添加到我原来的问题中。也许我在看什么?
  • 现在试试,我重构了你的代码以更接近这里的文档:contactform7.com/2015/03/28/custom-validation 同样在你更新的代码示例中,你已经在函数中包含了 add_filters,不确定这是否是故意的
  • 这实际上不是故意的。您的新代码也不起作用。想不通为什么。当我尝试您的代码时,它不会验证任何人的整个表单。控制台给了我这个错误:[Error] Failed to load resource: the server responded with a status of 500 (Internal Server Error) (admin-ajax.php, line 0)https://www.domain.nl/wp-admin/admin-ajax.php?action=wpcf7cf_validate_step
【解决方案2】:

好的,我用这段代码完成了:

function custom_postcode_validation_filter($result,$tag){

    $name = $tag->name;

    if($name == 'ba-postcode'){

        $bapostcode = $_POST['ba-postcode'];

        if($bapostcode != '') {

            if(!preg_match('/^([1-9]{1})([0-9]{3})([\s]{0,1})([a-zA-Z]{2})$/', $bapostcode)) {

                $result->invalidate( $tag, "Vul alstublieft een geldige postcode in." );

            }

        }

    }

    return $result;
}   

add_filter( 'wpcf7_validate_text*', 'custom_postcode_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_text', 'custom_postcode_validation_filter', 10, 2 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多