AngularJs check if date is valid


AngularJs check valid date : While working with dates in AngularJs we need to check whether the variable holds the valid date value or not. There are many ways to check for the valid date using AngularJs and JavaScript. You can use angular.isDate() to check a valid in AngularJs. Here in this tutorial we are going to explain how you can use AngularJs function angular.isDate() to check a date value. You can also use our online tool to run and edit the code.


AngularJs check valid date Example

You can check valid date as below-

JavaScript Part –

JavaScript Part Contains the following script –

AngularJs check valid date : JavaScript

 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
  $scope.result1 = '';
  $scope.result2 = '';
  $scope.dateModel = new Date();
  $scope.chackDate1 = function() {	 
    var d = new Date($scope.dateModel);
    if(angular.isDate(d)){
		$scope.result1= true;
	}else{
		$scope.result1= false;
	}
  }
  $scope.chackDate2 = function() {	 
    var d1 = "01-03-2016";
    if(angular.isDate(d1)){
		$scope.result2= true;
	}else{
		$scope.result2= false;
	}
  }
});
</script>

The above function will test the date if date is invalid it will return false else it will return true.

Html Part-

Html Part Contains the following html –

AngularJs check valid date: Html Part

<div ng-app="myApp">  
<div ng-controller="myController">  
	<input type="text" ng-model="dateModel"  /><br>
    <button ng-click ="chackDate1()" >Check Date</button>
	<br>
	Result = {{result1}}<br>
  <button ng-click ="chackDate2()" >Check Date</button>
	<br>
	Result = {{result2}}
</div> 
</div>

Complete Example-

Now let us combine both javascript and html parts. Here is the complete example –

How to check date is valid in AngularJs : Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
  $scope.result1 = '';
  $scope.result2 = '';
  $scope.dateModel = new Date();
  $scope.chackDate1 = function() {	 
    var d = new Date($scope.dateModel);
    if(angular.isDate(d)){
		$scope.result1= true;
	}else{
		$scope.result1= false;
	}
  }
  $scope.chackDate2 = function() {	 
    var d1 = "01-03-2016";
    if(angular.isDate(d1)){
		$scope.result2= true;
	}else{
		$scope.result2= false;
	}
  }
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
	<input type="text" ng-model="dateModel"  /><br>
    <button ng-click ="chackDate1()" >Check Date</button>
	<br>
	Result = {{result1}}<br>
  <button ng-click ="chackDate2()" >Check Date</button>
	<br>
	Result = {{result2}}
</div> 
</div>
</body>  
</html>

                                                                                    

Try it »

If you run the above example it will produce output something like this –

AngularJs check if date is valid


Advertisements

Add Comment

📖 Read More