Calendar(달력)
개발을 시작한지 얼마 안되었을때
회사에서 프로젝트를 진행하면서 javascript 달력을 만들었는데
오늘 보니 코드가 아주 더럽다.
좀더 깔끔하게 코드도 바꾸고 기능도 추가해 달력을 구현해 보았다.
먼저 달력구현을 위해 객체를 만들었다.
참고
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date
https://msdn.microsoft.com/ko-kr/library/4d4x3w61(v=vs.94).aspx
Calendar
//Calendar 객체
function Calendar(newDate){
if(newDate == null){
this.date = new Date();
}
this.date = newDate;
this.set = function(newDate) {
this.date = newDate;
};
this.get = function() {
return this.date;
};
this.getLongTime = function() {
return this.date.getTime();
};
this.setLongTime = function(millisec) {
return this.date.setTime(millisec);
};
this.setTxtTime = function(time_txt){
var time = time_txt.split(":");
this.date.setHours(time[0]);
this.date.setMinutes(time[1]);
this.date.setSeconds(time[2]);
}
this.getTxtTime = function(){
return this.date.setHours() + ":" + this.date.setMinutes() +":" +this.date.setSeconds();
}
this.addDay = function( day ) {
this.setLongTime(this.getLongTime() + day * 86400000);
}
this.getYear = function() {
return this.date.getFullYear();
};
this.setYear = function(year) {
this.date.setFullYear(year);
};
this.getMonth = function() {
return this.date.getMonth()+1;
};
this.setMonth = function(month) {
this.date.setMonth(month-1);
};
this.getDayOfMonth = function() {
return this.date.getDate();
};
this.setDayOfMonth = function(dayOfMonth) {
this.date.setDate(dayOfMonth);
};
this.setDayOfWeek = function(dayOfWeek) {
var day = dayOfWeek - this.date.getDay();
this.addDay(day);
};
this.getDayOfWeek = function() {
return this.date.getDay();
};
this.getLastDay = function(){
return new Date(this.date.getFullYear(), (this.date.getMonth())+1, 0);
}
this.getStartDay = function(){
return new Date(this.date.getFullYear(), this.date.getMonth(), 1);
}
this.setDate = function( year, month, dayOfMonth ) {
this.setYear(year);
this.setMonth(month);
this.setDayOfMonth(dayOfMonth);
};
this.getyyyyMMdd = function() { return this.getYear() + "-" + filling(this.getMonth(), 2) + "-" + filling(this.getDayOfMonth(), 2); };
var filling = function(value, length) {
var result = "" + value;
for( var step = result.length; step < length; step++ ) {
result = "0" + result;
}
return result;
}
}
내/외국인 주민등록번호 검증 로직 적용 / 정규식
이번에 외국인 사용자가 추가 되면서 유효성 검사를 새로 수정했다. 화면에서는 검증 로직을 반영해서 하드하게 validation체크를 하고 서버단에서는 정규식을 이용해서 숫자만 체크할 수 있도록 했다.
##javascript
CODE <div class="collapse_wrapper"> <div class="collapse" id="basic"> <div class="card">
var validatePersonalNo = function(personalNo) {
var strXxx = "234567892345";
var intMagicKey = 11;
var intModeNum = 11;
var intKey = 0;
if (personalNo.length != 13) return false;
for (var i = 0; i < strXxx.length; i++) {
intKey += parseInt(personalNo.charAt(i)) * parseInt(strXxx.charAt(i));
}
if("5678".match(personalNo.charAt(6))){
//외국인일 경우 13
intMagicKey = 13;
}
intKey = intMagicKey - (intKey % intModeNum);
if(intKey > 9){
intKey-=10;
}
return (intKey == personalNo.charAt(12)) ? true : false;
};
if(!personalNo.matches("^(?:[0-9]{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1,2][0-9]|3[0,1]))-?[1-6][0-9]{6}$")){ throw new **exception("주민등록번호가 올바르지 않습니다."); }> 이 포스트 더보기
JsReport: ios에서 history.go(-1) 버그
history.go(-1) 사용 시 웹 브라우저에서는 아무 문제가 없지만
ios 웹뷰에서 뒤로가기가 되지 않고 하얀 화면이 나오는 경우가 있음.
특정 경우에 about:blank가 떨어지게 되는데
원인은 정확히 알 수 없음.
웹뷰에서 window history에 문제가 있는 모양임.
정확히는
a,b,c 메뉴가 있는데
a - > b -> c 이런식으로 location.href 를 이용해서 페이지를 이동시켰다.
문제는 a->b -> a -> b -> c 이렇게 갔을 때는 뒤로가기가 정상적으로 됐는데
a->b->c 이렇게 c 메뉴에 갔다가 뒤로가기를 할경우에 하얀화면이 뜨는것이었다.
테스트 해봤지만 정확한 원인을 찾지 못했음.
그래서
b에서 c로 페이지 이동전에
replaceState를 이용해서 history에 돌아올 url을 정상적으로
넣어주었더니 되돌아 올때 하얗게 되는 현상이 없어졌다. ( b 페이지에 .. )
뭔가 페이지 이동할때 history가 깨지는 것 같다. (왜일까… )
var path = window.location.href;
var ua = navigator.userAgent.toLowerCase(),
version = 0;
if( this.isIos() ){
version = parseFloat(ua.substr(ua.search(/ipad|iphone/), 30).match(/\d+_+\d/)[0].replace('_', '.'));
if( version >= 9.3 ){
window.history.replaceState({}, "title" , path );
}
}
https://developer.mozilla.org/en-US/docs/Web/API/History_API
이방법은 살짝 꼼수라고 할 수 있는 듯.
정확한 원인은 시간있을때 알아봐야겠다 .