2016-12-29 95 views
0

我有兩個輸入端和我需要驗證各字段具有下列限制:B值必須總是輕微比值一個否則顯示出錯誤消息。這是example角:表單驗證和定製指令

我知道我需要創建一個指示,但我不知道,甚至如何下手。

HTML

<form name="exampleForm"> 
    <label>Value A</label> 
    <input type="number" name="valueA" data-ng-model="valueA" required="required"/> 
    <div ng-messages="exampleForm.valueA.$error"> 
     <div ng-message="required">This field is required</div> 
    </div> 
    <p> 
    <label>Value B</label> 
    <input type="number" name="valueB" data-ng-model="valueB" required="required"/> 
    <div ng-messages="exampleForm.valueB.$error"> 
     <div ng-message="required">This field is required</div> 
    </div> 
</form> 
+0

[看一看的文檔(https://docs.angularjs.org/guide/forms#custom-validation),還有自定義驗證的例子這會讓你走上正軌。 – Bigood

回答

0
<input type="number" name="valueB" data-ng-model="valueB" max="{{ valueA - 1 }}" required="required"/> 
    <div ng-messages="exampleForm.valueB.$error"> 
    <div ng-message="required">This field is required</div> 
    <div ng-message="max">This field is too large</div> 
    </div>