如何使用javascript仅计算下拉列表中的选定选项

时间:2018-05-16 07:40:53

标签: wordpress contact-form-7

如何在联系表单7 wordpress中的多选列表中实现自定义验证?

2 个答案:

答案 0 :(得分:0)

add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );

function custom_select_validation_filter( $result, $tag ) {
    if ( 'classtype-1' == $tag->name ) {
        if($_POST['menu-123'] =='Nursery to III'){
        $selected_options = isset( $_POST['classtype-1'] ) ?  $_POST['classtype-1'] : '';
        if(count($selected_options) < 2){
            $result->invalidate( $tag, "Select Atleast 2 options." );
        }
        }
    }

    if ( 'classtype-2' == $tag->name ) {
        if($_POST['menu-123'] =='IV to VIII'){
            $selected_options = isset( $_POST['classtype-2'] ) ?  $_POST['classtype-2'] : '';
        if(count($selected_options) < 2){
            $result->invalidate( $tag, "Select Atleast 2 options." );
        }
        }
    }
    return $result;
}

答案 1 :(得分:-1)

将以下代码放在functions.php文件中以验证选择输入

add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );

function custom_select_validation_filter( $result, $tag ) {
    if ( 'classtype' == $tag->name ) {
        $selected_options = isset( $_POST['classtype'] ) ?  $_POST['classtype'] : '';
        if(count($selected_option) < 2){
            $result->invalidate( $tag, "Select Atleast 2 options." );
        }
    }

    return $result;
}