duanaoshu1989 2015-02-03 14:07
浏览 16
已采纳

协助表单和JavaScript

I will like an assistance with the following.

I have three input the first one is Identity number , second one is Date of birth and the third one is gender. What I want is when I insert an Identity number on the first field to automatic insert dateofbirth and gender. South African ID numbers contain date of birth and gender. All I want is when I insert ID number, Birthdate and gender to automatic insert

Sample of identity number is 140101 5000 089 - The first six number is date of birth start with yymmdd and the second number is if a number start with 0-4 its a female and if its start with 5-9 its a male

First name: <input type="number" name="id_number" maxlength="13"><br>
Birthdate: <input type="text" class="datepicker" name="birthdate"><br>
Gender: <input type="text" name="gender"><br>
  • 写回答

2条回答 默认 最新

  • dsuoedtom207012191 2015-02-03 14:31
    关注

    You need to remove "type=number" from your first input. Otherwise it won't allow to type in spaces. Then you can split the ID into an array of words and use them.

    var $input1 = $("input[name=id_number]"),
        $input2 = $("input[name=birthdate]"),
        $input3 = $("input[name=gender]");
    
    $input1.on("keyup", function(){
            
        var id = $input1.val().split(" "),
            birthdate="",
            gender="",
            part1 = id[0],
            part2 = id[1];
        
        if(part1.length>5) birthdate = part1.substr(0,2) + "/" + part1.substr(2,2) + "/" + part1.substr(4,2);
        
        $input2.val(birthdate);
        
        if(part2 && part2.length){
             if(parseInt(part2[0]) <5)
                 gender = "Female"
             else
                 gender = "Male"
          
            $input3.val(gender);
        }
        
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    Identity number : <input type="text" name="id_number" maxlength="13"><br>
    Birthdate: <input type="text" class="datepicker" name="birthdate"><br>
    Gender: <input type="text" name="gender"><br>

    http://jsfiddle.net/as6rdtb1/

    Hope it helps.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算