如何在javascript中将字符串转换为逻辑运算符的数字?

时间:2015-06-09 18:26:41

标签: javascript angularjs kendo-ui

我有日期选择器,我在其中设置开始和结束日期的验证,因此如果非投掷错误,结束日期应始终大于开始日期... 我在比较中看到将字符串转换为数字的问题。

到目前为止尝试过代码

main.html中

<div class="form-group col-md-6 fieldHeight">
    <label for="cycleStart" class="col-md-4 required">Cycle Start</label>
    <div class="col-md-8">
        <input kendo-date-picker
        type="text" class="form-control" id="cycleStart"
        name="cycleStart"
        ng-model="rcsaCycleDTO.cycStartDate" 
        k-ng-model="cycleStrtObj"
        onkeydown="return false;"
        ng-change="validateDate"
        required data-required-msg="Cycle start is required">
        <p class="text-danger" ng-show="createRcsaCycleForm.cycleStart.$touched && createRcsaCycleForm.cycleStart.$error.required">Cycle Start is required field</p>                                
    </div>
<div class="form-group col-md-6 fieldHeight">
    <label for="cycleEnd" class="col-md-4 required">Cycle end (due date)</label>
    <div class="col-md-8">
        <input 
        kendo-date-picker
        type="text" class="form-control" id="cycleEnd"
        name="cycleEnd"
        ng-model="rcsaCycleDTO.cycEndDate"
        k-min="cycleStrtObj" 
        k-rebind="cycleStrtObj"
        onkeydown="return false;"
        ng-change="validateDate"
        required>
        <p class="text-danger" ng-show="createRcsaCycleForm.cycleEnd.$touched && createRcsaCycleForm.cycleEnd.$error.required">Cycle End is required field</p>
        <p class="text-danger" ng-show="createRcsaCycleForm.cycleEnd.$error.dateRange"> Cycle due date can not be less than cycle start date</p>
    </div>
</div>

main.js

$scope.validateDate = function(){
          var startDate = Date.parse($scope.rcsaCycleDTO.cycStartDate);
          var endDate =  Date.parse($scope.rcsaCycleDTO.cycEndDate);
          if(startDate > endDate) {
            $scope.createRcsaCycleForm.cycleEnd.$setValidity('dateRange',false);
          } else {
            $scope.createRcsaCycleForm.cycleEnd.$setValidity('dateRange',true);
          }
        }

0 个答案:

没有答案