使用其他字段和输入字段进行复选框验证

时间:2014-07-26 12:31:12

标签: javascript php forms validation

我是网页设计的新手。

我有联系表格,我想验证表格。当前的javascript在其他领域运行良好,但我遇到了服务领域。

这里我要验证是否选择了至少一个服务,如果选中“其他”选项,则旁边的输入字段不应为空白。

表单的HTML代码(服务字段的一部分)是:

<form action="" method="post" name="contactus" id="contactus" onsubmit="return validate_contactus()" autocomplete="off">

   <input type="checkbox" name="service[]" id="service[]" value="Complete new set up of Histopathology Laboratory">Complete new set up of Histopathology Laboratory<br>
   <input type="checkbox" name="service[]" id="service[]" value="Standardization of Histopath techniques">Standardization of Histopath techniques<br>
   <input type="checkbox" name="service[]" id="service[]" value="Training of technical staff">Training of technical staff<br>
   <input type="checkbox" name="service[]" id="service[]" value="Trouble shooting in day to day work">Trouble shooting in day to day work<br>
   <input type="checkbox" name="service[]" id="service[]" value="Addition of new techniques">Addition of new techniques<br>
   <input type="checkbox" name="service[]" id="service[]" value="Hands on training">Hands on training<br>
   <input type="checkbox" name="service[]" id="service[]" value="Seminars & Workshops in Histopathology">Seminars & Workshops in Histopathology<br>
   <input type="checkbox" name="service[]" id="service[]" value="Preparations of documents for NABL Audits">Preparations of documents for NABL Audits<br>
   <input type="checkbox" name="service[]" id="service[]" onclick="enable_text(this.checked)"  value="other" >Other

   <input type="text" name="other_text" id="other_text" autocomplete="off" ><br>

我试过的javascript是:

var chks = document.contactus.getElementsByName('service[]');
var checkCount = 0;
    for (var i = 0; i < chks.length; i++)
    {
      if (chks[i].checked)
    {
    checkCount++;
    }
    }
    if (checkCount < 1)
    {
      alert("Please select at least One.");
      return false;
    }


    if (document.contactus.getElementById("other").checked && document.contactus.getElementsByName('other_text').value ==="") {
        alert("More Details About Other Service You..... ");
        return false;
    }

另外,如何通过PHP代码添加所选服务(如果检查了多个)?我正在通过电子邮件发送这封联系表目前我使用的是:

Contact Person Name :  <?=$_POST['name']?><br><br>
Email :  <?=$_POST['email']?><br><br>
Address : <?=$_POST['address']?><br><br>
Mobile :  <?=$_POST['mobile']?><br><br>
Service Needed :  <?=$_POST['service']?><br><br>
Message : <br><?=$_POST['message']?>

0 个答案:

没有答案