2017-06-19 79 views
0

我使用MVC與角JS和使用parsley.js(版本2.6.2)parsley.js條件形式驗證

驗證當我檢查checkbox1然後2文本框(txtCustomer,txtchk1 & txtchk2)應該被要求提交表單時

當checkbox2然後點擊文本框(txtCustomer & txtchk3)應要求

我的形式如下。

  <input type="checkbox" id="chkCreditCard" ng-model="dvCreditCard" ng-change="alert1(dvCreditCard)" /> 

      Check1 required 
      <input ng-model="" type="text" class="form-control" id="txtchk1" placeholder="requied if checkbox1 checked" /> 
      <input ng-model="" type="text" class="form-control" id="txtchk2" placeholder="requied if checkbox1 checked" /> 


      <input type="checkbox" id="chkCheque" ng-model="dvCheque" /> 
      Check2 required 
      <input ng-model="" type="text" class="form-control" id="txtchk3" placeholder="requied if checkbox2 checked" /> 

    </form> 

回答

1

你可以使用ng-required指令這裏進行實地需要基於表達式

<input type="checkbox" id="chkCreditCard" ng-model="dvCreditCard"/> 

Check1 required 
<input ng-model="texchk1" ng-required="dvCreditCard" type="text" 
class="form-control" id="txtchk1" placeholder="requied if checkbox1 checked" /> 
<input ng-model="txtchk2" type="text" class="form-control" ng-required="dvCreditCard" 
    id="txtchk2" placeholder="requied if checkbox1 checked" /> 
<input type="checkbox" id="chkCheque" ng-required="dvCreditCard" ng-model="dvCheque" /> 

Check2 required 
<input ng-model="txtchk3" type="text" ng-required="dvCheque" class="form-control" id="txtchk3" placeholder="requied if checkbox2 checked" />