将验证功能应用于所有TextFields和单选按钮

时间:2015-01-14 21:09:56

标签: javascript arrays function

我是Javascript的新手,无法弄清楚如何让我的功能同时适用于单选按钮和文本字段。

以下是表单的HTML代码

<form action="sendmail.php" method="post" name="cascader"
 onsubmit="prepareEventHandlers()" id="cascader">   
 <div class="TargetCenter">   <p><strong><span class="asterisk">*</span>Target Center</strong>  </p>
 <label>
   <input type="checkbox" name="TargetCountry" value="allCountries" id="TargetCountry" />
   All Countries</label>
 <label>
   <input type="checkbox" name="TargetCountry" value="France" id="TargetCountry" />
   France</label>
 <label>
   <input type="checkbox" name="CheckboxGroup1" value="Bolivia" id="CheckboxGroup1_1" />
   Bolivia</label>
 <label>
   <input type="checkbox" name="CheckboxGroup1" value="North America" id="CheckboxGroup1_2" />
   North America</label>
 <label>
   <input type="checkbox" name="CheckboxGroup1" value="United Kingdom" id="CheckboxGroup1_3" />
   United Kingdom</label>
 <label>
   <input type="checkbox" name="CheckboxGroup1" value="Baltics" id="CheckboxGroup1_4" />
   Baltics</label>
 <label>
   <input type="checkbox" name="CheckboxGroup1" value="Slovakia" id="CheckboxGroup1_5" />
   Slovakia</label>

 <label>
   <input type="checkbox" name="CheckboxGroup1" value="Sweden" id="CheckboxGroup1_6" />
   Sweden</label>

 <label>
   <input type="checkbox" name="CheckboxGroup1" value="Switzerland" id="CheckboxGroup1_7" />
   Switzerland</label>
 <br />   </div>  <!--end of Cascade Target-->  <div class="CascadeCategory">    <strong>
 <span class="asterisk">*</span>Cascade Category: </strong>    <label>
  <input type="radio" name="cascadeCategory" value="Process" id="CascadeCategory_0" />
  Process</label>    <label>
  <input type="radio" name="cascadeCategory" value="Training" id="CascadeCategory_1" />
  Training</label>    <label>
  <input type="radio" name="cascadeCategory" value="Knowledge" id="CascadeCategory_2"/> Knowledge</label>    <br />     </div> <!--end
  of Cascade Category-->  <div class="ProcessTitle"><strong><span
  class="asterisk">*</span>Process Title:    <input name="textfld"
  type="text" id="processTitle" iname="processTitle"
  onkeypress="checkFieldValue()"  />  </strong><span
  id="errorMessage"></span></div>  <!--end of Process Title-->  <div
  class="CascadeType">    <strong><span class="asterisk">*</span>Cascade
  Type:</strong> <label>   <input type="radio" name="cascadeType"
  value="Release" />   Release</label>    <label>
  <input type="radio" name="cascadeType" value="Update" id="CascadeType_1" />
  Update</label>    <label>
  <input type="radio" name="cascadeType" value="Reminder" id="CascadeType_2" />
  Reminder</label>    <br /> </div>   <!--end of Cascade Type--> <div class="QuickDescr">    <strong><span class="asterisk">*</span>Quick
 Description: </strong><br /> <br /><textarea name="textfld" cols="70%"
  rows="5" id="quickDescr"></textarea><span id="errorMessage2"></span>
  </div>   <!--end of Quick Description--> <div class="Details">
  <strong><span class="asterisk">*</span>Details: </strong><br /><br/>
 <textarea name="details" cols="70%" rows="10" id="details"></textarea> </div>
 <!--end of Description--> <div class="DueDate"> <strong><span class="asterisk">*</span>Due
 Date:</strong> <input type="text" class="Due" name="DueDate"
 placeholder="mm.dd.yyyy" />  <span
 class="DueDateFormat">(mm.dd.yyyy)</span></div> <!--end of Due date-->
 <br /> <br /> <br /> <input name="Submit" type="submit"
 class="CascadeButton" value="Send Cascade"  />

 <input type="reset" value="Clear Fields" class="ResetButton" />
 </form>

以下是我为processTitle文本字段申请的Javascript。

function prepareEventHandlers() {
    document.getElementById("cascader").onsubmit = function() {
        // prevent a form from submitting if no email.
        if (document.getElementById("processTitle").value == "") {
            document.getElementById("errorMessage").innerHTML = "Please enter a value";
            // to STOP the form from submitting
            window.scrollTo(0, 0);
            document.getElementById('processTitle').style.cssText = 'background-color: #f4fc99;';
            // to turn the field background color
            return false;
        } else {
            // reset and allow the form to submit
            document.getElementById("errorMessage").innerHTML = "";
            return true;
        }
    };
}
// when the document loads
window.onload =  function() {
    prepareEventHandlers();
};
// Changes the field color onFocus
function checkFieldValue() {
    if (document.getElementById("processTitle").value != "") {
            document.getElementById('processTitle').style.cssText = 'background-color: #FFF;';
            document.getElementById("errorMessage").innerHTML = "";
        }
    else document.getElementById('processTitle').style.cssText = 'background-color: #f4fc99;';
}

2 个答案:

答案 0 :(得分:2)

我添加了一个小功能来检查是否选中了其中一​​个单选按钮。请参阅javascript底部的checkRequiredRadioButtons。目前我只是将它与您现有的验证失败代码相关联。

function prepareEventHandlers() {
        document.getElementById("cascader").onsubmit = function() {
          // prevent a form from submitting if no email.
          if (document.getElementById("processTitle").value == "" || !checkRequiredRadioButtons('cascadeType')) {
            document.getElementById("errorMessage").innerHTML = "Please enter a value";
            // to STOP the form from submitting
            window.scrollTo(0, 0);
            document.getElementById('processTitle').style.cssText = 'background-color: #f4fc99;';
            // to turn the field background color
            return false;
          } else {
            // reset and allow the form to submit
            document.getElementById("errorMessage").innerHTML = "";
            return true;
          }
        };
      }
      // when the document loads
    window.onload = function() {
      prepareEventHandlers();
    };
     // Changes the field color onFocus
    function checkFieldValue() {
      if (document.getElementById("processTitle").value != "") {
        document.getElementById('processTitle').style.cssText = 'background-color: #FFF;';
        document.getElementById("errorMessage").innerHTML = "";
      } else document.getElementById('processTitle').style.cssText = 'background-color: #f4fc99;';
    }

    function checkRequiredRadioButtons(buttonsName) {
      var buttonSet = document.getElementsByName(buttonsName);

      for(i = 0; i < buttonSet.length; i++){
          if(buttonSet[i].checked == true)
            return true;
      }
      return false;
    }
<form action="sendmail.php" method="post" name="cascader" onsubmit="prepareEventHandlers()" id="cascader">
  <div class="TargetCenter">
    <p><strong><span class="asterisk">*</span>Target Center</strong> 
    </p>
    <label>
      <input type="checkbox" name="TargetCountry" value="allCountries" id="TargetCountry" />All Countries</label>
    <label>
      <input type="checkbox" name="TargetCountry" value="France" id="TargetCountry" />France
    </label>
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="Bolivia" id="CheckboxGroup1_1" />Bolivia
    </label>
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="North America" id="CheckboxGroup1_2" />North America</label>
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="United Kingdom" id="CheckboxGroup1_3" />United Kingdom</label>
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="Baltics" id="CheckboxGroup1_4" />Baltics
    </label>
    <label>
      <input type="checkbox" name="CheckboxGroup1" value="Slovakia" id="CheckboxGroup1_5" />Slovakia
    </label>

    <label>
      <input type="checkbox" name="CheckboxGroup1" value="Sweden" id="CheckboxGroup1_6" />Sweden
    </label>

    <label>
      <input type="checkbox" name="CheckboxGroup1" value="Switzerland" id="CheckboxGroup1_7" />Switzerland
    </label>
    <br />
  </div>
  <!--end of Cascade Target-->
  <div class="CascadeCategory"> <strong>
 <span class="asterisk">*</span>Cascade Category: </strong> 
    <label>
      <input type="radio" name="cascadeCategory" value="Process" id="CascadeCategory_0" />Process
    </label>
    <label>
      <input type="radio" name="cascadeCategory" value="Training" id="CascadeCategory_1" />Training
    </label>
    <label>
      <input type="radio" name="cascadeCategory" value="Knowledge" id="CascadeCategory_2" />Knowledge</label>
    <br />
  </div>
  <!--end
  of Cascade Category-->
  <div class="ProcessTitle"><strong><span
  class="asterisk">*</span>Process Title:    <input name="textfld"
  type="text" id="processTitle" iname="processTitle"
  onkeypress="checkFieldValue()"  />  </strong><span id="errorMessage"></span>
  </div>
  <!--end of Process Title-->
  <div class="CascadeType"> <strong><span class="asterisk">*</span>Cascade
  Type:</strong> 
    <label>
      <input type="radio" name="cascadeType" value="Release" />Release</label>
    <label>
      <input type="radio" name="cascadeType" value="Update" id="CascadeType_1" />Update
    </label>
    <label>
      <input type="radio" name="cascadeType" value="Reminder" id="CascadeType_2" />Reminder
    </label>
    <br />
  </div>
  <!--end of Cascade Type-->
  <div class="QuickDescr"> <strong><span class="asterisk">*</span>Quick
 Description: </strong>
    <br />
    <br />
    <textarea name="textfld" cols="70%" rows="5" id="quickDescr"></textarea><span id="errorMessage2"></span>
  </div>
  <!--end of Quick Description-->
  <div class="Details">
    <strong><span class="asterisk">*</span>Details: </strong>
    <br />
    <br/>
    <textarea name="details" cols="70%" rows="10" id="details"></textarea>
  </div>
  <!--end of Description-->
  <div class="DueDate"> <strong><span class="asterisk">*</span>Due
 Date:</strong> 
    <input type="text" class="Due" name="DueDate" placeholder="mm.dd.yyyy" /> <span class="DueDateFormat">(mm.dd.yyyy)</span>
  </div>
  <!--end of Due date-->
  <br />
  <br />
  <br />
  <input name="Submit" type="submit" class="CascadeButton" value="Send Cascade" />

  <input type="reset" value="Clear Fields" class="ResetButton" />
</form>

如果您正在尝试动态确定是否有文本框或单选按钮,则可以调用Element.getAttribute('type')并进行比较。如:

var allInputs = document.getElementsByTagName('input');
for(i = 0; i < allInputs.length; i++){
    if(allInputs[i].getAttribute('type') == 'radio'){
        //Do radio button handling
    } else if(allInputs[i].getAttribute('type') == 'text'){
        //Do textbox handling  
    }
}

但是,如果你这样做,你需要注意,对于单选按钮,你将迭代组中的所有单选按钮,检查和取消选中。以及提交和重置按钮。

答案 1 :(得分:1)

也许您应该尝试使用javascript getElementsByTagName() method

function myFunction() {
 var x = document.getElementsByTagName("input");
 for (var i=0; i<x.length; i++){
  if(x[i].value==""){
   x[i].style.backgroundColor="red";
  }
 }

}