JQuery or Javascript function for MOD 10 algorithm
What will be the Javascript or JQuery code for following scenario ?
"A ABA TextBox=Mandatory field".
Must be numeric and 9 digits. Perform MOD 10 algorithm
- Multiply each digit of the first 8 digits of the ABA/Routing number with the following numbers respectively. a. 3,7,1,3,7,1,3,7
- Add the results of the 8 multiplications.
- Subtract the sum from the next highest multiple of 10.
- The result of the above step 3 must be equal to the 9th digit of the ABA/Routing number. If not display error in Message section
I have 9 different textboxes for each digit of ABA number . First I have to check if the entered number is numeric or not then I have to add this valid number in a String Array onwhich finally i will be adding my ABA Number validation .
I am absolutely stuck . Dont know how to solve this thing . I have found few codes but which doesnt qualify my need
EDIT :This is my HTML Code OnKeyUp Event I am adding value of perticular textbox in a string Array and OnKeyPress I am checking if the entered number is numeric or not
<div style="padding-bottom: 5px; padding-left: 200px;padding-right: 20px;padding-top: 0;vertical-align: middle; width: 500px;">
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab1" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab2" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab3" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab4" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab5" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab6" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab7" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab8" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="text" maxlength="1" size="10px" style="width:15px" id="ab9" onkeypress ="return onlyNumbers();" onkeyup ="return addABNumbers();" />
<input type="hidden" id="finalABNumber" value="" onkeyup="return onlyNumbers();" />
</div>
<script language="javascript" type="text/javascript">
function addABNumbers() {
var finalabNumber = document.getElementById("ab1").value + document.getElementById("ab2").value +
document.getElementById("ab3").value +
document.getElementById("ab4").value +
document.getElementById("ab5").value +
开发者_StackOverflow中文版 document.getElementById("ab6").value +
document.getElementById("ab7").value +
document.getElementById("ab8").value +
document.getElementById("ab9").value;
alert(finalabNumber);
document.getElementById('finalABNumber').value = finalabNumber;
} function onlyNumbers() {
// debugger;
var chek=true;
//var finalNumber = document.getElementById('finalAcntNumber').value;
//document.getElementById("finalAcntNumber").value =
var finalNumber =
document.getElementById("ac1").value +
document.getElementById("ac2").value +
document.getElementById("ac3").value +
document.getElementById("ac4").value +
document.getElementById("ac5").value +
document.getElementById("ac6").value +
document.getElementById("ac7").value +
document.getElementById("ac8").value +
document.getElementById("ac9").value +
document.getElementById("ac10").value +
document.getElementById("ac11").value +
document.getElementById("ac12").value +
document.getElementById("ac13").value +
document.getElementById("ac14").value +
document.getElementById("ac15").value+
document.getElementById("ac16").value;
// debugger;
var test = document.getElementById("ac1").value;
// chek = IsNumeric(document.getElementById("ac1").value);
// alert(chek);
// if (finalNumber.length < 15 )
// if(chek=false)
// alert('not a valid');
// alert(finalNumber);
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
else if (isNaN(document.getElementById('finalAcntNumber').value)) {
alert('not a valid');
}
else {
// debugger;
// if (test != "") {
// alert(finalNumber);
document.getElementById("Text1").value = finalNumber;
// }
return true;
}
// if (!isNaN(document.getElementById("finalAcntNumber").value))
}
</script>
I figured out the answer on my own and the correct answer as per my requirement is as follows
<script>
function validation {
var input = $("#someTextBox").val();
if (!isNaN(input)) {
var values = {
input.charAt(0) * 3,
input.charAt(1) * 7,
input.charAt(2) * 1,
input.charAt(3) * 3,
input.charAt(4) * 7,
input.charAt(5) * 1,
input.charAt(6) * 3,
input.charAt(7) * 7,
}
var sum = 0;
for (int i = 0; i < values.length; i ++) {
sum += values[i];
}
var modTen = parseInt(sum / 10);
var result = (modTen + 1) * 10;
var finalResult = result - sum;
if (finalResult == (input.charAt(8)))
alert("Valid ABA Number.");
else
alert("Error");
}
</script>
I wouldn't split your input into 8 boxes; just use one.
However, you should look into the javascript isNan function; that will help you validate on numbers. The rest of the steps you listed are quite simple. What else are you stuck on?
精彩评论