Skip to content
This repository has been archived by the owner on May 12, 2018. It is now read-only.

#109 Partial enhancement: Implemented masking date and time as user s… #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/components/mdpDatePicker/mdpDatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,55 @@ module.directive("mdpDatePicker", ["$mdpDatePicker", "$timeout", function($mdpDa
scope.$on("$destroy", function() {
inputElement.off("reset input blur", onInputElementEvents);
});

if(scope.dateFormat == "MM/DD/YYYY") {
inputElement.on("keypress", formatDateAsTyped);
}

function formatDateAsTyped(event) {
var localInputElement = this;
var inputDate = localInputElement.value;
var unicode = event.keyCode ? event.keyCode : event.charCode;

//Allow BackSpace, Tab, F5, Del, left arrow, right arrow in firefox. IE, chrome, safari supports these keys
if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 116 || event.keyCode == 46 || (event.keyCode == 37 && !event.charCode) || event.keyCode == 39) {
return true;
}

if (isNumber(unicode) && inputDate.length < 10) {
//replaces 2 to 02, 3 to 03.. for month field
inputDate = inputDate.replace(/^([2-9])/, "0$1");
//restrict typing 13, 14, 15, 16, 17, 18, 19 for month field
if(inputDate == "1" && (unicode >= 51 && unicode <= 57)) {
return false;
}
//replaces 4 to 04, 5 to 05.. for date field
inputDate = inputDate.replace(/^([0-9])([0-9])(\/)([4-9])/, "$1$2$30$4");
//restrict typing 2,3,4,5,6,7,8,9 in date field second position
if(inputDate.length == 4 && inputDate.charAt(3) == '3' && (unicode >= 50 && unicode <= 57)){
return false;
}
if (inputDate.length == 2 || inputDate.length == 5) {
inputDate = inputDate + "/";
}
localInputElement.value = inputDate;
return true;
}
else {
return false;
}
}

function isNumber(unicode) {
var char = String.fromCharCode(unicode);

if ((("0123456789").indexOf(char) > -1)) {
return true;
}
else {
return false;
}
}
}
}
};
Expand Down
46 changes: 46 additions & 0 deletions src/components/mdpTimePicker/mdpTimePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,52 @@ module.directive("mdpTimePicker", ["$mdpTimePicker", "$timeout", function($mdpTi
scope.$on("$destroy", function() {
inputElement.off("reset input blur", onInputElementEvents);
})

if(scope.timeFormat == "HH:mm") {
inputElement.on("keypress", format24HourTimeAsTyped);
}


function format24HourTimeAsTyped(event) {
var localInputElement = this;
var p = localInputElement.value;
var unicode = event.keyCode ? event.keyCode : event.charCode;

//Allow BackSpace, Tab, F5, Del, left arrow, right arrow in firefox. IE, chrome, safari supports these keys
if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 116 || event.keyCode == 46 || (event.keyCode == 37 && !event.charCode) || event.keyCode == 39) {
return true;
}

if (isNumber(unicode) && p.length < 5 ){
//replaces 3 to 03, 4 to 04..
p = p.replace(/^([3-9])/, "0$1");
//restrict typing 24, 25, 26, 27, 28, 29 in hours
if(p == "2" && (unicode >= 52 && unicode <= 57)) {
return false;
}
if (p.length == 2) {
p = p + ":";
}
//restrict typing 6, 7, 8, 9 in minutes first position
if(p.length == 3 && (unicode >= 54 && unicode <= 57)){
return false;
}
localInputElement.value=p;
return true;
} else {
return false;
}
}

function isNumber(unicode) {
var char = String.fromCharCode(unicode);

if ((("0123456789").indexOf(char) > -1)) {
return true;
} else {
return false;
}
}
}
};
}]);
Expand Down