I am trying to write a java program that verifies the validity of a bank account number in Algeria, and I was checking the web for formula, and I found a piece of javascript (that I could not understand).
Here is the code:
function validateFormRIB() {
var x = document.forms["formrib"]["rib"].value;
if ((!/^\d+$/.test(x))||(x.length!=20)) {
alert("Le RIB doit comporter exactement 20 caractères numériques");
return false;
}
}
My question is what is the formula used for the computation?
Here is the link to the web page : http://www.babalweb.net/finance/verificateur-rib-releve-identite-bancaire-algerie.php
The "formula" you refer to is (I presume) this part
(!/^\d+$/.test(x))||(x.length!=20)
It has two parts:
(!/^\d+$/.test(x))
and
(x.length!=20)
The first part is a pattern match. It testing to see if the string in the variable x
matches the regular expression /^\d+$/
. That regex means
/ # start of regular expression
^ # match the start of the string
\d # match one digit
+ # the previous pattern is matched one or more times
$ # match the end of the string
/ # end of regular expression.
In other words, "match a string that consists of one or more digits".
The second part is testing the string length.
Putting it all together, in context:
IF variable 'x' does not consist of one or more digits
OR IF it does not have a length of 20
THEN show and 'alert' popup and return false.
The validation "formula" is easy to translate into Java ... or any other language with regex support.
However, it should be obvious that not every 20 digit number is actually a real Algerian bank account number.
(For what it is worth, the Wikipedia page on International Bank Account Numbers (IBANs) has a summary of formats for a number of countries. But it doesn't say anything more about Algerian bank numbers except that they are 20 digits.)