我有两个输入字段,一个是From Year
,另一个是To Year
。仅当To Year
大于From Year
值时,我才需要验证表单的提交按钮。
这是来自年份的代码:
<div class="col-md-6 no-pad-right">
<label>Tenure From Year</label>
<input name="tenure_from_year" ng-model="joinFormData.tenureFromYear" name="tenure_from_year" class="form-control" placeholder="2014" type="text" ng-required="joinFormData.memberType == 'alumni'" ng-maxlength="4" ng-pattern="/(?:(?:19|20)[0-9]{2})/">
<span ng-show="joinForm.tenure_from_year.$error.pattern || joinForm.tenure_from_year.$error.maxlength">
<span class="red-color">Enter correct year!</span>
</span>
</div>
这是年份的代码:
<div class="col-md-6 no-pad-right">
<label>Tenure To Year</label>
<input name="tenure_to_year" ng-model="joinFormData.tenureToYear" name="tenure_to_year" class="form-control" placeholder="2014" type="text" ng-required="joinFormData.memberType == 'alumni'" ng-maxlength="4" ng-pattern="/(?:(?:19|20)[0-9]{2})/">
<span ng-show="joinForm.tenure_to_year.$error.pattern || joinForm.tenure_to_year.$error.maxlength">
<span class="red-color">Enter correct year! </span>
</span>
</div>
如果表单无效,则提交按钮将被禁用
<button type="submit" class="btn btn-default btn-save-custom" ng-click="joinLeap()" ng-disabled="joinForm.$invalid" >Submit</button>
因此,我如何验证从和到年份字段,以便在年份有效时启用提交按钮。
答案 0 :(得分:0)
您可以使用手表。
$scope.validate = false;
$scope.$watch('joinFormData.tenureFromYear', function (newVal, oldVal) {
if (newVal > oldVal) {
$scope.validate = true;
} else {
$scope.validate = false;
}
});
答案 1 :(得分:0)
比较您在ng-model
中提到的方法joinLeap()
中的ng-click
字段
$scope.dateValidation = false;
$scope.joinLeap = function (joinFormData) {
if (joinFormData.tenureToYear > joinFormData.tenureFromYear) {
$scope.dateValidation = true;
} else {
$scope.dateValidation = false;
}
}
通过ng-click
方法传递参数
<button type="submit" class="btn btn-default btn-save-custom" ng-click="joinLeap(joinFormData)" ng-disabled="joinForm.$invalid || !dateValidation" >Submit</button>
我想这会帮助您。根据要求为dateValidation设置true,false值。