简体   繁体   中英

dutch Bank account validation rule

Can anybody advise me how to make a validation rule for Dutch bank accounts?

So far i could only found this on web:

regex = /[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{3}/;

This is my JavaScript:

function dutchBankAccount(input) {
var regex = /[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{2}[\\s]{1}[0-9]{3}/;
if(input.value.toString().match(regex) && !(input.value == "")) {
    return true;
} else {
    input.click();
    input.style.border = '2px solid #F20056';
    return false;
}

}

And here is my HTML code:

<li><input type="text" id="anum" placeholder="Account Number" autocomplete="off" onkeypress="return isNumberKey(event)" onBlur="isValidAnum()" onFocus="emptyAnum('anum')"/></li>

Later on when I enter a dutch bank account I get error which I'm not supposed to get. So if you know how to solve this please help me.

The regex syntax is incorrect. Try something more like this:

var regex = /[0-9]{2}\s[0-9]{2}\s[0-9]{2}\s[0-9]{3}/;

That matches strings like

32 01 28 192

Two digits followed by a space three times, then three digits. Whether that's what all Dutch bank accounts look like I don't know, though that seems like a small namespace for something like that.

(It occurs to me that /(?:\d{2}\s){3}\d{3}/ should match the same strings and it's a little shorter.)

edit — To elaborate, the regex in the original code has some problems:

  • The backslashe before each of the "\s" (space) characters is doubled, but it should not be. (That's assuming that Dutch bank account numbers don't actually look like "92\s31\28s\120")
  • Putting a single character class shortcut ("\s") in square brackets is needlessly redundant
  • Suffixing a regex element with "{1}" is needlessly redundant too

The real problem was the extra backslash. Also, speaking of needlessly redundant, there's no need to call ".toString()" on the value of an input element "value" attribute, and there's no need to make sure the value isn't the empty string if it has matched the pattern. In this case, an empty string cannot match the pattern, so that test is not necessary. Finally (promise), if you're just testing a regex against a string, the ".test()" method on the RegExp prototype is a little more efficient:

if (regex.test(input.value)) { // matched
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM