Need help for JS error

LKuser

Active member
  • Jan 2, 2017
    509
    113
    43
    Colombo
    Have a look on this NIC to DOB calculator. It says how it work and there button to check out ID. It works fine for my ID. But some users reported issues.

    Examples 1: It converts 732130006V as Year = 1973, Month = July & Day = 31. Actually, the result should have been Month = August & Day = 1.

    Examples 2: It converts 199706003780 as Month = February and Day = 29. But, 1997 is not leap year, and result should be Month = March & Day = 1.
     
    • Like
    Reactions: D_Mad and samsri

    RavinduSha

    Well-known member
  • Aug 24, 2014
    1,100
    658
    113
    Kurunegala
    They are taking February as a month with 29 days each year without considering whether it's a leap year or not. that's how the NIC is generated if I remember correctly.
     
    • Like
    Reactions: D_Mad

    LKuser

    Active member
  • Jan 2, 2017
    509
    113
    43
    Colombo
    Where is the source code?

    JavaScript:
    <script>
        $(document).ready(function () {
            $("#find").click(function () {
                //Clear Existing Details
                $("#error").html("");
                $("#gender").html("");
                $("#year").html("");
                $("#month").html("");
                $("#day").html("");
    
                var NICNo = $("#nic").val();
                var dayText = 0;
                var year = "";
                var month = "";
                var day = "";
                var gender = "";
                if (NICNo.length != 10 && NICNo.length != 12) {
                    $("#error").html("Invalid NIC No.");
                } else if (NICNo.length == 10 && !$.isNumeric(NICNo.substr(0, 9))) {
                    $("#error").html("Invalid NIC No.");
                }
                else {
                    // Year
                    if (NICNo.length == 10) {
                        year = "19" + NICNo.substr(0, 2);
                        dayText = parseInt(NICNo.substr(2, 3));
                    } else {
                        year = NICNo.substr(0, 4);
                        dayText = parseInt(NICNo.substr(4, 3));
                    }
    
                    // Gender
                    if (dayText > 500) {
                        gender = "Female";
                        dayText = dayText - 500;
                    } else {
                        gender = "Male";
                    }
    
                    // Day Digit Validation
                    if (dayText < 1 && dayText > 366) {
                        $("#error").html("Invalid NIC No.");
                    } else {
    
                        //Month
                        if (dayText > 335) {
                            day = dayText - 335;
                            month = "December";
                        }
                        else if (dayText > 305) {
                            day = dayText - 305;
                            month = "November";
                        }
                        else if (dayText > 274) {
                            day = dayText - 274;
                            month = "October";
                        }
                        else if (dayText > 244) {
                            day = dayText - 244;
                            month = "September";
                        }
                        else if (dayText > 213) {
                            day = dayText - 213;
                            month = "Auguest";
                        }
                        else if (dayText > 182) {
                            day = dayText - 182;
                            month = "July";
                        }
                        else if (dayText > 152) {
                            day = dayText - 152;
                            month = "June";
                        }
                        else if (dayText > 121) {
                            day = dayText - 121;
                            month = "May";
                        }
                        else if (dayText > 91) {
                            day = dayText - 91;
                            month = "April";
                        }
                        else if (dayText > 60) {
                            day = dayText - 60;
                            month = "March";
                        }
                        else if (dayText < 32) {
                            month = "January";
                            day = dayText;
                        }
                        else if (dayText > 31) {
                            day = dayText - 31;
                            month = "Febuary";
                        }
    
                        // Show Details
                        $("#gender").html("Gender : " + gender);
                        $("#year").html("Year : " + year);
                        $("#month").html("Month : " + month);
                        $("#day").html("Day :" + day);
                    }
                }
            });
        });
    </script>
     
    • Like
    Reactions: D_Mad

    RavinduSha

    Well-known member
  • Aug 24, 2014
    1,100
    658
    113
    Kurunegala
    I don't see any error in it. It is working as expected. As I mentioned before they take every year as a leap year when generating NIC. Therefore February has 29days in all the NIC numbers.
     
    • Like
    Reactions: D_Mad and LKuser

    LKuser

    Active member
  • Jan 2, 2017
    509
    113
    43
    Colombo
    I don't see any error in it. It is working as expected. As I mentioned before they take every year as a leap year when generating NIC. Therefore February has 29days in all the NIC numbers.

    Yes, you are correct. Is there any fuction in JS to find leap year?
     
    • Like
    Reactions: D_Mad