본문 바로가기
Programming/Javascript

javascript로 14세 미만 체크하기

by Berasix 2022. 10. 28.
반응형

javascript에서 14세 미만 체크하기

	function checkAge(birth) {
		var date = new Date();
		var year = date.getFullYear();
		var month = (date.getMonth() + 1);
		var day = date.getDate();       
		var result = false;

		if (month < 10) month = '0' + month;
		if (day < 10) day = '0' + day;

		var monthDay = month + '' + day;
		birth = birth.replace('-', '').replace('-', '');
		var birthdayy = birth.substr(0, 4);
		var birthdaymd = birth.substr(4, 4); 
		var age = monthDay < birthdaymd ? year - birthdayy - 1 : year - birthdayy;

		if ( age < 14) result = true;
		else if( monthDay < birthdaymd ) result = true;

		return result;
	}
    
    checkAge('2002-01-01')
728x90

댓글