-3

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>
Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
  • whats the question? can you post your code. Also, so many tags with no justification – atmd Feb 03 '15 at 14:09
  • 1
    Time to learn about substr or regular expressions. – epascarello Feb 03 '15 at 14:14
  • You need to split your identity number and then interpret the tokens obtained, for more help see this answer http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript – Amitd Feb 03 '15 at 14:19

2 Answers2

2

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.

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
0

First retrive the number from the form (I have modified html a bit)

<input type="text" name="id_number" id="IdentityNumber" maxlength="13">

string identityNumber = document.getElementById("IdentityNumber");

then you can reuse the logic as used in the answer below for extracting the fields from it..
(date of birth and gender)

South African ID Number Validate and Get Age and Gender

Then assign them back to the respective textboxes.
(as per the @Elgringorrible answer above.)

Community
  • 1
  • 1
Amitd
  • 4,769
  • 8
  • 56
  • 82