Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple range validation added #6

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.bower.json
*.swp
9 changes: 5 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "angular-input-date",
"description": "AngularJS directive to enable support for input[type=\"date\"]",
"version": "1.0.4",
"name": "angular-input-date-extended rangeValidation",
"description": "AngularJS directive to enable support for input[type=\"date\"] added range validation, fork of betsol/angular-input-date",
"version": "1.0.8",
"main": "src/angular-input-date.js",
"homepage":"https://github.com/sergicollado/angular-input-date",
"dependencies": {
"angular": "~1.2.26"
},
"devDependencies": {
}
}
}
23 changes: 14 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# angular-input-date 1.0.4
# angular-input-date 1.0.5

This module provides a very simple directive to enable support for `input[type="date"]` with the latest stable version of AngularJS (`~1.2.26`).

Expand Down Expand Up @@ -84,16 +84,21 @@ application.controller('AppCtrl', function($scope, someExistingDate, inputDate)
You have to inject our service called `inputDate` and invoke it's `ExtractDate` method.
It will return proper timeless `Date` object.

## Feedback
## Ranges validation

If you have found a bug - please create an issue in this GitHub repository.

If you have a question - file it with [StackOverflow][so-ask] and send me a
link to [[email protected]][email].
``` html
<input type="date" date-options min-limit="startDate" max-limit="endDate" ng-model="myDateObject">
```

Have any ideas or propositions? Feel free to contact me by the same [E-Mail address][email].
You can specify scope value in your controller like this:

Cheers!
``` javascript
application.controller('AppCtrl', function($scope) {
$scope.myDateObject = new Date(2014, 3, 19);
$scope.startDate = new Date(2014, 3, 16);
$scope.endDate = new Date(2014, 3, 23);
});
```

## License

Expand Down Expand Up @@ -122,4 +127,4 @@ THE SOFTWARE.
[mdn-date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
[demo]: http://jsfiddle.net/slavafomin/F2LcY/4/
[so-ask]: http://stackoverflow.com/questions/ask?tags=angularjs,javascript
[email]: mailto:[email protected]
[email]: mailto:[email protected]
115 changes: 98 additions & 17 deletions src/angular-input-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

var inputDateFormat = 'yyyy-MM-dd';

/**
* Check varible or object to 'is undefined', 'is empty' or 'is null'
* @param obj
*/
angular.isUndefinedOrNullOrEmpty = function (obj) {
return angular.isUndefined(obj) || obj === null || obj.length === 0 || typeof obj === 'object' && Object.keys(obj).length === 0;
};

/**
* Converts string representation of date to a Date object.
*
Expand Down Expand Up @@ -44,33 +52,106 @@
);
}

/**
* Check variable is instace of Date and return Date obj *
* @param date
* @constructor
*/
var formatDate = function(date){
if(!(date instanceof Date) && angular.isDefined(date)){
date = new Date(date);
}
return date;
};

/**
* Valide selected date is between range *
* I.e. truncates time part.
* @param selectedDate,minLimit,maxLimit
* @constructor
*/
function validationRange(selectedDate,minLimit, maxLimit){
var dateToCompare = selectedDate;
if(angular.isUndefined(dateToCompare)) {
return true;
}

dateToCompare = formatDate(selectedDate);
minLimit = formatDate(minLimit);
maxLimit = formatDate(maxLimit);

var minLimitError = (angular.isDefined(minLimit) && minLimit && dateToCompare < minLimit);
var maxLimitError = (angular.isDefined(maxLimit) && maxLimit && dateToCompare > maxLimit);

if(!dateToCompare || minLimitError || maxLimitError){
return false;
}
return true;
}

/**
* Valide selected date required
* @param selectedDate,isRequired
* @constructor
*/
function validationRequired(selectedDate, isRequired) {
if(!isRequired || angular.isUndefined(isRequired)){
return true;
}
return !(angular.isUndefinedOrNullOrEmpty(selectedDate));
}

angular.module('ngInputDate', ['ng'])
.factory('inputDate', function() {
return {
ExtractDate: ExtractDate
};
})
.directive('input', ['dateFilter', function(dateFilter) {
.directive('dateOptions',function(){

return {
restrict: 'A',
scope:{
isRequired: '=?ngRequired',
minLimit: '=',
maxLimit: '='
},
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$setValidity('required',validationRequired(ngModel.$modelValue, scope.isRequired));

ngModel.$formatters.push(function(modelValue) {
ngModel.$setValidity('required',validationRequired(modelValue, scope.isRequired));
ngModel.$setValidity('range', validationRange(modelValue, scope.minLimit, scope.maxLimit));
return modelValue;
});
ngModel.$parsers.push(function(viewValue) {
ngModel.$setValidity('required',validationRequired(viewValue, scope.isRequired));
ngModel.$setValidity('range', validationRange(viewValue, scope.minLimit, scope.maxLimit));
return viewValue;
});

}
};
})
.directive('input', ['dateFilter','inputDate', function(dateFilter, inputDate) {
return {
restrict: 'E',
require: '?ngModel',
link: function(scope, element, attrs, ngModel) {
if (
'undefined' !== typeof attrs.type
&& 'date' === attrs.type
&& ngModel
) {
ngModel.$formatters.push(function(modelValue) {
return dateFilter(modelValue, inputDateFormat);
});

ngModel.$parsers.push(function(viewValue) {
return parseDateString(viewValue);
});
if (angular.isUndefined(attrs.type) ||
'date' !== attrs.type || !ngModel || !inputDate.enabled) {
return;
}
ngModel.$formatters.push(function(modelValue) {
return dateFilter(modelValue, inputDateFormat);
});

ngModel.$parsers.push(function(viewValue) {
return parseDateString(viewValue);
});
}
}
}])
;
};
}]) ;

})(window, angular);
})(window, angular);