0

How can I do date control validation in an AngularJS form for the following code ?

<label for="startDate" class="control-label">Start Date:</label>
<div class="">
    <input type="date" class="form-control" 
           id="startDate" ng-model="startDate" />
</div>

<label for="endDate" class="control-label">End Date:</label>
<div class="">
    <input type="date" class="form-control" 
           id="endDate" ng-model="endDate" />
</div>

<div class="alert alert-warning" ng-show="">
    // ??
</div>

If only one of the dates are entered, then the form should prompt to enter the other date. If both dates are empty, or both are entered, then it should successfully submit the form.

4

4 に答える 4

0

あらゆる種類の日付検証に対してこのスコープ関数を呼び出すことができます。ng-submit で dateValidator(year,month,date) をトリガーするようにしてください

$scope.dateValidator = function(year, month, date) {

  var dates = new Date(year, month - 1, date);
  if (dates.getFullYear() == year && dates.getMonth() + 1 == month && dates.getDate() == date) {
    alertify.success('Valid Date');

  } else {
    alertify.error('Invalid date');
    return false;
  }
};
于 2015-12-07T06:55:29.570 に答える
0

これはあなたの質問を解決するための私のアプローチです

HTML

<label for="startDate" class="control-label">Start Date:</label>
<div class="" >
    <input type="date" class="form-control" 
           id="startDate" ng-model="startDate" />
</div>

<label for="endDate" class="control-label">End Date:</label>
<div class="">
    <input type="date" class="form-control" 
           id="endDate" ng-model="endDate" />

</div>
<span>{{errMessage}}</span>
    <button ng-click='checkErr(startDate,endDate)'>Submit</button>

このコードを HTML のコントローラーに追加します

$scope.checkErr = function(startDate,endDate){
    $scope.errMessage = '';

    if(!startDate && endDate){
      $scope.errMessage = 'please Enter Start Date';
      return false;
    }
    if(!endDate && startDate){
       $scope.errMessage = 'please Enter End Date';
       return false;
    }
    if(endDate && startDate || !endDate && !startDate ){
       $scope.errMessage ='Successfully Submitted';
       // you can add you submit function here  
    }
  };

いずれかの日付が入力され、一方が残っている場合、送信ボタンの上にエラー メッセージがスローされます。ユーザーが入力する必要がある日付が表示され、両方の日付が入力されている場合、または両方の日付が入力されていない場合は、正常に送信されたと表示されます。if ケースに送信機能を追加できます。

それが役に立てば幸い

于 2015-06-01T21:13:31.453 に答える
0

コントローラーを取り付けてこれを外す必要があります。$scope.startDate が空かどうかを確認します。!empty の場合、$scope.endDate は !empty である必要があり、それ以外の場合はエラーになりません。

` $scope.startDate = null; $scope.endDate = null; if( !$scope.startDate ) {

if(!$scope.endDate) { $scope.error = false }

} else { if( !$scope.endDate ) { $scope.error = true; $scope.reason = "有効な終了日を入力してください。" } }`

于 2015-06-01T20:21:28.470 に答える