2013-07-09 60 views
0

我在需要使用Coldfusion驗證的網頁上有複選框,我如何驗證每個組中至少有一個複選框需要回答?使用Coldfusion驗證複選框

<div name="multanswerheader" id="multanswerheader" style="width:100%;"> 
      <div name="multanswerques" id="multanswerques"> 
       <label class="multanswerques">Ques 1:</label> 
       <label class="multanswerques">Ques 2:</label> 
       <label class="multanswerques">Ques 3:</label> 
       <label class="multanswerques">Ques 4:</label> 
       <label class="multanswerques">Ques 5:</label> 
      </div> 
      <div name="multanswera" id="multanswera"> 
       <div class="multanswerques"><input type="checkbox" name="multradio1" value="1" <cfif ListFind(form.multradio1,"1") GT 0>checked="checked"</cfif>>A</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio2" value="1" <cfif ListFind(form.multradio2,"1") GT 0>checked="checked"</cfif>>A</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio3" value="1" <cfif ListFind(form.multradio3,"1") GT 0>checked="checked"</cfif>>A</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio4" value="1" <cfif ListFind(form.multradio4,"1") GT 0>checked="checked"</cfif>>A</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio5" value="1" <cfif ListFind(form.multradio5,"1") GT 0>checked="checked"</cfif>>A</input></div> 
      </div> 
      <div name="multanswerb" id="multanswerb"> 
       <div class="multanswerques"><input type="checkbox" name="multradio1" value="2" <cfif ListFind(form.multradio1,"2") GT 0>checked="checked"</cfif>>B</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio2" value="2" <cfif ListFind(form.multradio2,"2") GT 0>checked="checked"</cfif>>B</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio3" value="2" <cfif ListFind(form.multradio3,"2") GT 0>checked="checked"</cfif>>B</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio4" value="2" <cfif ListFind(form.multradio4,"2") GT 0>checked="checked"</cfif>>B</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio5" value="2" <cfif ListFind(form.multradio5,"2") GT 0>checked="checked"</cfif>>B</input></div> 
      </div> 
      <div name="multanswerc" id="multanswerc"> 
       <div class="multanswerques"><input type="checkbox" name="multradio1" value="3" <cfif ListFind(form.multradio1,"3") GT 0>checked="checked"</cfif>>C</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio2" value="3" <cfif ListFind(form.multradio2,"3") GT 0>checked="checked"</cfif>>C</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio3" value="3" <cfif ListFind(form.multradio3,"3") GT 0>checked="checked"</cfif>>C</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio4" value="3" <cfif ListFind(form.multradio4,"3") GT 0>checked="checked"</cfif>>C</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio5" value="3" <cfif ListFind(form.multradio5,"3") GT 0>checked="checked"</cfif>>C</input></div> 
      </div> 
      <div name="multanswerd" id="multanswerd"> 
       <div class="multanswerques"><input type="checkbox" name="multradio1" value="4" <cfif ListFind(form.multradio1,"4") GT 0>checked="checked"</cfif>>D</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio2" value="4" <cfif ListFind(form.multradio2,"4") GT 0>checked="checked"</cfif>>D</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio3" value="4" <cfif ListFind(form.multradio3,"4") GT 0>checked="checked"</cfif>>D</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio4" value="4" <cfif ListFind(form.multradio4,"4") GT 0>checked="checked"</cfif>>D</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio5" value="4" <cfif ListFind(form.multradio5,"4") GT 0>checked="checked"</cfif>>D</input></div> 
      </div> 
      <div name="multanswere" id="multanswere"> 
       <div class="multanswerques"><input type="checkbox" name="multradio1" value="5" <cfif ListFind(form.multradio1,"5") GT 0>checked="checked"</cfif>>E</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio2" value="5" <cfif ListFind(form.multradio2,"5") GT 0>checked="checked"</cfif>>E</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio3" value="5" <cfif ListFind(form.multradio3,"5") GT 0>checked="checked"</cfif>>E</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio4" value="5" <cfif ListFind(form.multradio4,"5") GT 0>checked="checked"</cfif>>E</input></div> 
       <div class="multanswerques"><input type="checkbox" name="multradio5" value="5" <cfif ListFind(form.multradio5,"5") GT 0>checked="checked"</cfif>>E</input></div> 
      </div>     
     </div> 

回答

3

這裏是我做過的一個表格的一個例子。

<cfif isDefined("form.submit")> 
    <cfif form.checkbox1a EQ "" > 
     <p class="text-error">Please check at least one answer below</p> 
    </cfif> 
</cfif> 

<fieldset data-role="controlgroup"> 
    <legend>*Pick some chips</legend> 
    <input type="checkbox" name="checkbox1a" id="checkbox1a" value="a" <cfif ListFind(form.checkbox1a,"a") GT 0>checked="checked"</cfif>/> 
    <label for="checkbox1a">Cheetos</label> 
    <input type="checkbox" name="checkbox1a" id="checkbox2a" value="b" <cfif ListFind(form.checkbox1a,"b") GT 0>checked="checked"</cfif>/> 
    <label for="checkbox2a">Doritos</label> 
    <input type="checkbox" name="checkbox1a" id="checkbox3a" value="c" <cfif ListFind(form.checkbox1a,"c") GT 0>checked="checked"</cfif>/> 
    <label for="checkbox3a">Fritos</label> 
    <input type="checkbox" name="checkbox1a" id="checkbox4a" value="d" <cfif ListFind(form.checkbox1a,"d") GT 0>checked="checked"</cfif>/> 
    <label for="checkbox4a">Sun Chips</label> 
</fieldset> 

您必須確保您的複選框具有相同的名稱,但具有不同的值。然後只需檢查一下該名稱是否有值。我在複選框之前執行了我的操作,因爲我希望錯誤消息出現在框的上方,但由於CF僅在表單提交時觸發,因此它將執行第二次。

+0

你知道如果form.checkbox1a沒有在表單中提交,這段代碼會錯誤的嗎?未經檢查的複選框不會被提交,也不是表單範圍的一部分。 –

+0

我不太清楚你的意思是什麼......我有這些工作的形式,因爲我希望它。 –

+0

@Zorpix - 那是因爲你在代碼的其他地方聲明瞭'form.checkbox1a'的默認值。所以它總是被定義的。刪除默認(和額外的'ListFind'代碼),如果沒有選中任何一個框,上面的代碼將會出錯。 – Leigh

1

你應該在客戶端做這件事,而不是在ColdFusion中。未選中的複選框不會提交給服務器。您將不得不測試它們不屬於服務器上表單範圍的事實。