luhn algorithm c# valid sa id code example
Example: south african id number validation c#
<script>
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
$('#idCheck').click(function () {
$('#error p').remove();
var error = $('#error');
var idNumber = $('#check').val();
var correct = true;
if (idNumber.length != 13 || !isNumber(idNumber))
{
error.append('<p>ID number does not appear to be authentic</p>');
correct = false;
}
var tempDate = new Date(idNumber.substring(0, 2), idNumber.substring(2, 4) - 1, idNumber.substring(4, 6));
var id_date = tempDate.getDate();
var id_month = tempDate.getMonth();
var id_year = tempDate.getFullYear();
var fullDate = id_date + "-" + id_month + 1 + "-" + id_year;
if (!((tempDate.getYear() == idNumber.substring(0, 2)) && (id_month == idNumber.substring(2, 4) - 1) && (id_date == idNumber.substring(4, 6)))) {
correct = false;
}
var genderCode = idNumber.substring(6, 10);
var gender = parseInt(genderCode) < 5000 ? "Female" : "Male";
var citzenship = parseInt(idNumber.substring(10, 11)) == 0 ? "Yes" : "No";
var tempTotal = 0;
var checkSum = 0;
var multiplier = 1;
for (var i = 0; i < 13; ++i) {
tempTotal = parseInt(idNumber.charAt(i)) * multiplier;
if (tempTotal > 9) {
tempTotal = parseInt(tempTotal.toString().charAt(0)) + parseInt(tempTotal.toString().charAt(1));
}
checkSum = checkSum + tempTotal;
multiplier = (multiplier % 2 == 0) ? 1 : 2;
}
if ((checkSum % 10) != 0) {
correct = false;
};
if (correct) {
error.css('display', 'none');
$('#result').empty();
$('#result').append('<p>South African ID Number: ' + idNumber + '</p><p>Birth Date: ' + fullDate + '</p><p>Gender: ' + gender + '</p><p>SA Citizen: ' + citzenship + '</p>');
}
else {
error.css('display', 'block');
}
});
</script>
<div class="form-group">
<label>ID Number:<b style="color:red; ">*</b></label>
<div class="input-group mb-3">
<input type="text" name="ID" id="check" class="form-control" required >
<div class="input-group-append">
<button class="btn btn-primary" type="button" id="idCheck">Validate</button>
</div>
</div>
<div id="result" style="width:100%; background-color:rgba(58, 114, 59, 0.48); padding:5px; border-radius:5px; color:white; "></div>
<div id="error" style="width:100%; background-color:rgba(219, 23, 23, 0.48); padding:5px; border-radius:5px; color:white; "></div>
</div>