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

后端 未结 7 2197
盖世英雄少女心
盖世英雄少女心 2020-12-08 22:56

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

相关标签:
7条回答
  • 2020-12-09 00:03

    To allow any amount of spaces anywhere:

    ^ *D *E( *[A-Za-z0-9]){20} *$
    

    As you want to allow lower letters, also DE might be lower?

    ^ *[Dd] *[Ee]( *[A-Za-z0-9]){20} *$
    
    • ^ matches the start of the string
    • $ end anchor
    • in between each characters there are optional spaces *
    • [character class] defines a set/range of characters

    To allow at most one space in between each characters, replace the quantifier * (any amount of) with ? (0 or 1). If supported, \s shorthand can be used to match [ \t\r\n\f] instead of space only.

    Test on regex101.com, also see the SO regex FAQ

    0 讨论(0)
提交回复
热议问题