Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

whitespace - Regex for IBAN allowing for white spaces AND checking for exact length

I need to check an input field for a German IBAN. The user should be allowed to leave in white spaces and input should be validated to have a starting DE and then exact 20 characters numbers and letters.

Without the white space allowance, I tried

^[DE]{2}([0-9a-zA-Z]{20})$

but I cannot find where and how I can add "white spaces anywhere allowed.

This should be simple, but I simply cannot find a solution.

Thanks for help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because you should use the right tool for the right task: you should not rely on regexps to validate IBAN numbers, but instead use the IBAN checksum algorithm to check the whole code is actually correct, making any regexp superfluous and redundant. i.e.: remove all spaces, rearrange the code, convert to integers, and compute remainder, here it's best explained.

Though, there am I trying to answer your question, for the fun of it:

what about:

^DE([0-9a-zA-Z]s?){20}$

which only difference is allowing a whitespace (or not) after each occurence of a alphanumeric character.

here is the visualization:

Regular expression visualization

edit: for the OP's information, the only difference is that this regexp, from @ulugbex-umirov: (?:s*[0-9a-zA-Z]s*) does a lookahead check to see if there's a space between the iso country code and the checksum (which only made of numerical digits), which I do not support on purpose.

And actually to support a correct IBAN syntax, which is formed of groups of 4 characters, as the wikipedia page says:

^DEd{2}s?([0-9a-zA-Z]{4}s?){4}[0-9a-zA-Z]{2}$

Regular expression visualization

example

If your UI is in Javascript, you can use that library for doing IBAN validation:

<script src="iban.js"></script>
<script>
    // the API is now accessible from the window.IBAN global object
    IBAN.isValid('hello world'); // false
    IBAN.isValid('BE68539007547034'); // true
</script>

so you know this is a valid IBAN, and can validate it before the data is ever even sent to the backend. Simpler, lighter and more elegant… Why do something else?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...