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

how to count only selected options in dropdown list using javascript

如何在 contact form 7 wordpress 的多选列表中实现自定义验证?

将以下代码放入您的 functions.php 文件中以验证 select 输入

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;
}
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;
}