验证隐藏的多复选框/输入字段

Posted

技术标签:

【中文标题】验证隐藏的多复选框/输入字段【英文标题】:Validation of Hidden Multi-Checkbox/Input fields 【发布时间】:2017-11-23 11:54:18 【问题描述】:

为了处理带有多个可选复选框的字段验证,我使用了 webshims polyfill。

表单具有隐藏/取消隐藏表单部分的复选框。

最初的任务是仅在隐藏部分变得可见时验证/要求隐藏部分。

我最初的想法是使用 webshims polyfill 的功能,仅在选中与使该字段可见相关联的复选框时才需要隐藏字段。

问题 #1: 一些隐藏部分包括这些需要验证的多复选框类型字段。如何让这些字段利用 webshims 的 data-grouprequired="" 功能,以便验证这些多复选框字段?

问题 #2: 有一个隐藏部分 (Registries/id=div3) 包含 5 个输入文本字段 - 最终用户只需填写这些字段中的至少一个。我该如何做到这一点,当这个隐藏部分变得可见时,至少需要 5 个字段中的一个来验证表单?

或者,如果有更好的解决方案,我很想学习。我对此很陌生。

这是表单中的相关代码:

   <script>
     webshim.setOptions('forms', 
        lazyCustomMessages: true,
        addValidators: true
    );

    webshim.polyfill('forms forms-ext');
    </script>

    <script>
    function addTableRow() 
        var $tableRow = $('tr.model-row:first-child');
        var $clonedRow = $tableRow.clone().show();
        $('#stmember').append($clonedRow);
        

    function myDeleteFunction() 
        var $memberTRs = $('tr', '#stmember');
        // If rowcount === 1, hide first row, don't remove it!!
        var rowCount = $memberTRs.length;
        if (rowCount === 1) 
            $('tr.model-row:first-child').hide();
            return;
        
        $memberTRs.last().remove();
        

        jQuery(function() 

        $('#delete').click(function() 
            myDeleteFunction();
        );

        $('#add').click(function() 
            addTableRow();
        );

    );
    </script>

    <script type="text/javascript">
    function showMe (it, box) 
        var vis = (box.checked) ? "block" : "none";
        document.getElementById(it).style.display = vis;
    
    </script>
<body>
<form method="post" action="mailto:you@example.com" enctype="text/plain">
    <div class="row 50%">
        <div class="12u">
        Which of the following would you like to do with your specified patient population? Check all that apply.<br />
            <ul class="list">
                <li><input type="checkbox" id="recruit" name="todo" value="1" data-grouprequired="" onclick="showMe('div1', this)"> Recruitment for research</li>
                <li><input type="checkbox" id="edc" name="todo" value="2" onclick="showMe('div2', this)"> Electronic Data Capture</li>
                <li><input type="checkbox" id="registry" name="todo" value="3" onclick="showMe('div3', this)"> Registry</li>
                <li><input type="checkbox" id="dextract" name="todo" value="4" onclick="showMe('div4', this)"> Data Extraction</li> 
                <li><input type="checkbox" id="ptpop" name="todo" value="5">Estimate of number of patients in target population</li>    
            </ul>
        </div>
    </div>
<div id="div1" style="display:none">
    <div class="row 50%">
        <div class="12u">
            <h3><strong>Recruitment</strong></h3>
            What methods would you like to use for patient recruitment? Check all that apply.<br />
                <ul class="list">
                    <li><input type="checkbox" name="vehicle" value="Bike"> Patient Notification Through MyChart</li>
                    <li><input type="checkbox" name="vehicle" value="Car"> Study Team Member Notification When Appropriate Patient Checks In</li>
                    <li><input type="checkbox" name="vehicle" value="Car"> Provider Notification During Patient Encounter</li>
                </ul>
            </div>
        </div>  
    </div>      
    <div id="div2" style="display:none">
        <h3><strong>Electronic Data Capture</strong></h3>
        <div class="row 50%">
            <div class="6u 12u (mobile)">
            How would you like to capture data on your patient population? Check all that apply.<br />
            <ul class="list" style="margin-bottom: 0px; padding-bottom: 0px;">
                <li><input type="checkbox" name="vehicle" value="Bike"> Patient entry through MyChart</li>
                <li><input type="checkbox" name="vehicle" value="Car"> Patient entry through Welcome</li>
                <li><input type="checkbox" name="vehicle" value="Car"> Patient entry through REDCap</li>
                <li><input type="checkbox" name="vehicle" value="Car"> Clinician entry through EPIC</li>    
                <li><input type="checkbox" name="vehicle" value="Car"> Clinician entry through REDCap</li>
                <li><input type="checkbox" name="vehicle" value="Car"> MHi-GO (Mobile Application)</li> 
            </ul>
        </div>
        <div class="6u 12u(mobile)">
        Where would you like the data delivered to? (Server name, share name, or JHBox, Enterprise NAS, etc.)
            <input name="subname" type="text" data-dependent-validation='"from": "edc", "prop": "required"' />
        </div>
    </div>
    <div class="row 50%">
        <div class="6u 12u(mobile)">
        In what format would you like to receive your data? (Excel, pipe-delimited, CSV, SQL tables)
            <input name="subjhed" type="text" data-dependent-validation='"from": "edc", "prop": "required"' />
        </div>
        <div class="6u 12u(mobile)">
        Would you like the data visualized and if so how? (Graphs, tables, diagrams, other)
            <input name="subemail" type="text" data-dependent-validation='"from": "edc", "prop": "required"' />
        </div>
    </div>
</div>
<div id="div3" style="display:none">
    <div class="row 50%">
    <h3><strong>Registries</strong></h3>
    </div>
    <h3 class="quest-header"><strong>What information and/or metrics of your patient population would you like to track over time?</strong></h3>
    Please specify if applicable:
    <div class="row 50%">
            <div class="6u 12u(mobile)">
            Diagnoses:
                <input name="subjhed" type="text" />
            </div>
            <div class="6u 12u(mobile)">
            Problem List:
                <input name="subemail" type="text" />
            </div>
        </div>
        <div class="row 50%">
            <div class="6u 12u(mobile)">
            Medications:
                <input name="subjhed" type="text" />
            </div>
            <div class="6u 12u(mobile)">
            Labs:
                <input name="subemail" type="text" />
            </div>
        </div>
        <div class="row 50%">   
            <div class="6u 12u(mobile)">
            Other:
                <input name="subjhed" type="text" />
            </div>
        </div>  
    </div>
    <div id="div4" style="display:none">
        <div class="row 50%">
            <div class="12u">
            <h3><strong>Data Extraction</strong></h3>
            What data would you like to extract?
            <textarea name="description" data-dependent-validation='"from": "dextract", "prop": "required"' ></textarea>
            </div>
        </div>
        <div class="row 50%">
            <div class="6u 12u(mobile)">
            Where would you like the data delivered to? (Server name, share name, or JHBox, Enterprise NAS, etc.)
                <input name="subemail" type="text" data-dependent-validation='"from": "dextract", "prop": "required"' />
            </div>
            <div class="6u 12u(mobile)">
            In what format would you like to receive your data? (Excel, pipe-delimited, CSV, SQL tables)
                <input name="subjhed" type="text" data-dependent-validation='"from": "dextract", "prop": "required"' />
            </div>
        </div>
    </div
        <div class="row 50%">
            <div class="12u">
            <center>
                <ul class="actions">
                    <li><input type="submit" value="Submit Request" /></li>
                    <li><input type="submit" value="Save & Continue Later" /></li>
                    <li><input type="reset" value="Clear form" /></li>
                </ul>
            </center>
            </div>
       </div>
  </form>

【问题讨论】:

【参考方案1】:

最终我自己想通了。禁用所有隐藏字段。让它们在变得可见时启用。对于多个文本字段,能够使用来自 webshims polyfill 的 data-grouprequired,但必须创建自定义错误消息。

【讨论】:

以上是关于验证隐藏的多复选框/输入字段的主要内容,如果未能解决你的问题,请参考以下文章

输入字段表单验证 - 复选框

统一启用隐藏移动输入后,输入字段不可编辑

如果选中相邻复选框,则输入字段验证

根据复选框条件验证输入文本字段(最大值和最小值)

仅当字段在 asp.net mvc 中可见时才需要字段验证

启用复选框时如何隐藏工具提示