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

时间:2014-06-24 15:24:14

标签: javascript jquery validation checkbox

我有一个具有多个值的表格,用于共同字段" service"。我创建了一个选项"其他"供选择。我想用"其他"旁边的javascript输入字段进行验证。选项。 代码如下

<label for="service" style="font-size:15px; vertical-align:top;">Service You Want*</label>

<input type="checkbox" name="service[]" id="Complete new set up " value="Complete new set up ">Complete new set up<br>
<input type="checkbox" name="service[]" id="Standardization" value="Standardization ">Standardization<br>
<input type="checkbox" name="service[]" id="Training" value="Training">Training <br>
<input type="checkbox" name="service[]" id="Trouble shooting " value="Trouble shooting ">Trouble shooting <br>
<input type="checkbox" name="service[]" id="Addition of new techniques" value="Addition of new techniques">Addition of new techniques<br>
<input type="checkbox" name="service[]" id="Hands on training" value="Hands on training">Hands on training<br>
<input type="checkbox" name="service[]" id="Seminars & Workshops" value="Seminars & Workshops">Seminars & Workshops<br>
<input type="checkbox" name="service[]" id="Preparations of documents" value="Preparations of documents ">Preparations of documents <br>
<input type="checkbox" name="service[]" id="Other" onclick="enable_text(this.checked)"  value="other" >Other

<input type="text" name="other_text"><br>

对于&#34;其他&#34;选项,输入文本字段获得启用,如果&#34;其他&#34;检查选项。

用于启用输入字段的工作javascript是:

<script>     
    function enable_text(status)
    {
        status=!status; 
        document.formname.other_text.disabled = status;
    }
</script>

javascript用于选择至少一个服务正在运行,它如下:

<script>
    var chks = document.getElementsByName('service[]');
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++)
    {
        if (chks[i].checked)
        {
            hasChecked = true;
            break;
        }
    }
    if (hasChecked == false)
    {
        alert("Please Select At Least One Service.");
        return false;
    }  
</script>

但是我没有得到如何验证输入字段,如果&#34;其他&#34;选中复选框中的选项。

1 个答案:

答案 0 :(得分:0)

尝试关注javascript:

<script>
var chks = document.getElementsByName('service[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please Select AtLeast One Service.");
return false;
}  
function isBlank(str) {
return (!str || /^\s*$/.test(str));
} 

if (document.getElementById("Other").checked &&  isBlank(document.getElementsByName('other_text').value)) {
alert("Please Enter Details ABout Other Services You Want......... ");
document.contactus.other_text.focus();
return false;
}
</script>