티스토리 뷰

JavaScript


주민번호 입력받고 남/녀, 성인/미성년 구분

정  의

☞ 텍스트 필드에 주민번호 앞자리 6자리( id = post )

☞ 패스워드 필드에 주민번호 뒷자리 7자리( id = pre )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        
        $('#post').on('keypress'function(event){
//             alert(event.which); // 0(숫자) == 48(이벤트 코드) ~ 9 == 57 // backspace == 8
            
            if(event.which && ( event.which > 47 && event.which <58 || event.which == 8)){}
            else
                {
                    event.preventDefault();
//                     return false;                
                }
        });
        $('#pre').on('keypress'function(event){
            
            if(event.which && ( event.which > 47 && event.which <58 || event.which == 8)){}
            else
                {
                    event.preventDefault();
//                     return false;                
                }
        });
        
        $('#post').on('keyup'function(){
            if($(this).val().length == 6)
                $('#pre').trigger('focus');
        });
        $('#pre').on('keyup'function(){
            if($(this).val().length == 7){
                var ch = $(this).val().slice(0,1);
                var sex = $('#sex');
                if(ch == '1' || ch == '3')
                    sex.text('남자');
                else if(ch == '2' || ch == '4')
                    sex.text('여자');
                
                var isAdult = $('#isAdult');
                var year = $('#post').val().slice(0,2);
                var age;
                if(ch == '1' || ch == '2')
                    //1900년대
                    age = 116 - year;
                else if (ch == '3' || ch == '4')
                    //2000년대
                    age = 16 - year;
                
//                 isAdult.text(age);
                    
                if(age > 19)
                    isAdult.text('성인');
                else
                    isAdult.text('미성년자');
            
            }        
                        
        });
        
        
    });
</script>
</head>
<body>
    <input type="text" maxlength="6" id="post">-<input type="password" maxlength="7" id="pre"><br>
    
    성별 : <span id="sex"></span><br>
    성인여부 : <span id="isAdult"></span><br>
</body>
</html>
cs






댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함
Total
Today
Yesterday