테스트 사이트 - 개발 중인 베타 버전입니다

자바스크립트 onkeyup시 자동 계산 채택완료

</p><p><?php  $toYear = date("ymd");  ?> </p><p>< script language = "javascript">  </p><p style="margin-left: 40px;">function birthShow() {    </p><p style="margin-left: 80px;"> var inputBirth = document.getElementById("mb_birth_year").value;    </p><p style="margin-left: 80px;"> var toYear = <?php echo $toYear; ?>;    var circulate = mb_birth_year.innerHTML = (inputBirth-toYear);   </p><p style="margin-left: 80px;"> </p><p style="margin-left: 80px;">  if(circulate < 14) {      </p><p style="margin-left: 120px;">  alert("회원 가입을 위해서는 만 14세 이상이어야 합니다.");              mb_birth_year.focus();   </p><p style="margin-left: 120px;">  } </p><p style="margin-left: 120px;"><span style="font-size: 11pt; line-height: 1.5;"> } </span></p><p style="margin-left: 120px;"><span style="font-size: 11pt; line-height: 1.5;"> </span></p><p style="margin-left: 120px;"><span style="font-size: 11pt; line-height: 1.5;">< /script> </span></p><p style="margin-left: 120px;"><span style="font-size: 11pt; line-height: 1.5;">< meta http-equiv="content-type" content="text/html;charset=utf-8" /> </span></p><p style="margin-left: 120px;"><span style="font-size: 11pt; line-height: 1.5;"> </span></p><p style="margin-left: 120px;"><span style="font-size: 11pt; line-height: 1.5;">< tr> </span></p><p style="margin-left: 160px;"><span style="font-size: 11pt; line-height: 1.5;">< th scope="row"><label for="mb_birth_year"><font color="red">*</font> Birth<strong class="sound_only"></strong></label></th> </span></p><p style="margin-left: 160px;"><span style="font-size: 11pt; line-height: 1.5;">< td></span></p><p style="margin-left: 160px;"><span style="font-size: 11pt; line-height: 1.5;"> <input type="text" name="mb_birth_year" id ="mb_birth_year" class="frm_input required nospace" onkeyup="birthShow();"></span></p><p style="margin-left: 160px;"><span style="font-size: 11pt; line-height: 1.5;"> < /td></span></p><p style="margin-left: 160px;"><span style="font-size: 11pt; line-height: 1.5;"> < /tr>  </span></p><p>

 

안녕하세요,

지금 간단한 자바스크립트를 짰는데 잘못된거같아 도움 좀 부탁드리겠습니다.

 

현재 제가 지금 할려는게, 회원가입을 할때 나이를 받는데, 나이를 입력할때 만 14세가 안되면 alert문이 띄워지면서 안되게 하는건데요..

위에 있는 소스대로 짜봤는데, 생각처럼 잘 되지가 않더군요..

 

일단 input박스에서는 mb_birth_year 로 네임과 아이디를 지정해줬고요, $toYear 라는 변수로 올해 년도를 지정했습니다.

 

그래서 유저가 input박스에 입력했을때 만약 만 14세가 되지 않았을 경우에는 alert이 띄우도록 했습니다.  해당 소스에서 부족한 부분좀 알려주시면 정말 감사하겠습니다.

 

그리고 input박스에서 onkeyup을 썼는데, 이걸 쓰니까 숫자를 다 넣기전에 저 함수가 실행되던데,

onkeypress, onkeydown, onkeyup 중에 어떤걸 써야 하나요? 만약 이게 아니라면 어떤걸 써서 만들어야 될지 스크립트 고수분들께 부탁 좀 드리겠습니다

댓글을 작성하려면 로그인이 필요합니다.

답변 2개

채택된 답변
+20 포인트

</p><p>function birthShow() {
   var inputBirth = document.getElementById("mb_birth_year").value;
   var Year = new Date();
   var toYear = Year.getFullYear();
   if((toYear- inputBirth) < 14) {
      alert("회원 가입을 위해서는 만 14세 이상이어야 합니다.");
      mb_birth_year.focus();
   }
}</p><p>

onblur로 바꿔서 해 보세요. 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

답변 정말 고맙습니다. 큰 도움이 되었습니다

댓글을 작성하려면 로그인이 필요합니다.

</p><p><?php  $toYear = date("Ymd");  ?> 
<script>  
function birthShow() {    
 var inputBirth = document.getElementById("mb_birth_year").value;    </p><p> var toYear = <?php echo $toYear; ?>;    
 var circulate = mb_birth_year.innerHTML = (toYear-inputBirth);   
 
  if(circulate < 140000) {      
  alert("회원 가입을 위해서는 만 14세 이상이어야 합니다.");              
  mb_birth_year.focus();   
  } 
 } 
 
</script> 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
 
<tr> 
<th scope="row"><label for="mb_birth_year"><font color="red">*</font> Birth<strong class="sound_only"></strong></label></th> 
<td>
 <input type="text" name="mb_birth_year" id ="mb_birth_year" class="frm_input required nospace" onblur="birthShow();">
 </td>
 </tr>  </p><p>

 

대소문자 구분하시고 뛰워쓰고 빈칸도 확인해야댑니다.

계산식이랑 전체적으로 확인해보세여

몇가지 수정했습니다

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

소중한 답변 감사합니다. 고맙습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인

전체 질문 목록