주민번호를 입력받아 성인인지와 남자인지 여자인지 판별하는 예제

( 앞텍스트창에서 6자리를 입력받으면 다음 텍스트창으로 포커스이동 )

( 텍스트창에서 문자의 입력을 막음 )



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js">
</script>
<script type="text/javascript">
$(function(){
$('#jumin1').on('keypress',function(event){
// alert(event.which); //눌러보니 48이 0이고 57이 9네요
if (event.which &&
(event.which > 47 && event.which < 58
|| event.which == 8)) {
} else
{
// event.preventDefault();
return false; // 이벤트를 끝내서 문자입력을 막음
}
});
$('#jumin1').on('keyup', function(){
// alert('되나?');
if($(this).val().length == 6)
$('#jumin2').trigger('focus');
});
$('#jumin2').on('keyup', function(){
if($(this).val().length == 7){
var isGender = $('#isGender')
var ch = $(this).val().slice(0,1);
if(ch==1 || ch ==3){
isGender.text("남자");
alert('결과가 나왔습니다.');
}
else if(ch==2 || ch ==4){
isGender.text("여자");
alert('결과가 나왔습니다.');
}
else
alert("주민번호 제대로 입력하세요.")
var isAdult = $('#isAdult');
var year = $('#jumin1').val().slice(0,2);
var age;
if( ch == '1' || ch == '2')
age = 116 - year;
else if (ch == '3' || ch == '4')
age = 16-year;
isAdult.text(age);
}
});
});
</script>
</head>
<body>
<input type="text" id="jumin1" size="6"> -
<input type="password" id="jumin2" size="7" maxlength="7"> <p/>
성별 : <span id="isGender"> <p/>
성인여부 : <span id="isAdult"></span> <p/>
</body>
</html>














'jQuery' 카테고리의 다른 글

jQuery - 04.Ajax ( 정의, 예제 )  (0) 2016.07.14
jQuery - 02.요소를 이용한 예제들  (0) 2016.07.13
jQuery - 01.정의 및 사용하기  (0) 2016.07.12
Posted by 보로로롬
,