0

I have this code to check if my fields are all good to go. I have my back-end up and working, but I'm struggling to check the regex inside my react method. All I did was created a regex in regex101 before used it on the input pattern, but I wanted to change it to the method. So basically the regex always returns false...

// Then check regex
const regInput = new RegExp(
  '^(?:(?:IT|SM)d{2}[A-Z]d{22}|CYd{2}[A-Z]d{23}|NLd{2}[A-Z]{4}d{10}|LVd{2}[A-Z]{4}d{13}|(?:BG|BH|GB|IE)d{2}[A-Z]{4}d{14}|GId{2}[A-Z]{4}d{15}|ROd{2}[A-Z]{4}d{16}|KWd{2}[A-Z]{4}d{22}|MTd{2}[A-Z]{4}d{23}|NOd{13}|(?:DK|FI|GL|FO)d{16}|MKd{17}|(?:AT|EE|KZ|LU|XK)d{18}|(?:BA|HR|LI|CH|CR)d{19}|(?:GE|DE|LT|ME|RS)d{18}|ILd{21}|(?:AD|CZ|ES|MD|SA)d{22}|PTd{23}|(?:BE|IS)d{24}|(?:FR|MR|MC)d{25}|(?:AL|DO|LB|PL)d{26}|(?:AZ|HU)d{27}|(?:GR|MU)d{28})$'
);

if (!regInput.test(this.state.iban)) {
  this.setState({
    error: true,
    errorMsg:
      'Sąskaitsssos numeris įvestas klaidingai, bandykite dar kartą',
  });
  console.log('error');
  return;
} else {
  console.log('LT597300010145601329');
}

enter image description here

Dziugito Bizox
  • 55
  • 1
  • 12

3 Answers3

1

According to this link here,

An IBAN, or international bank account number starts with a two-digit country code, then two numbers, followed by several more alphanumeric characters.

You can use the following regex:

^[a-zA-Z]{2}[\d]{2}[a-zA-Z0-9]{14,20}$

In short, this accepts a value with 2 letters(irrespective of the case) followed by 2 digits followed by alphanumeric characters ranging from 14-20 characters (You can change the length constraint if you have more details on the pattern).


Detailed explaination:

Match a single character present in the list below [a-zA-Z]

  • {2} matches the previous token exactly 2 times
  • a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive)
  • A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)

Match a single character present in the list below [\d]

  • {2} matches the previous token exactly 2 times

  • \d matches a digit (equivalent to [0-9])

Match a single character present in the list below [a-zA-Z0-9]

  • {14,20} matches the previous token between 14 and 20 times, as many times as possible, giving back as needed (greedy)

  • $ asserts position at the end of a line

nymphadora
  • 839
  • 11
  • 18
0

Seems like your regex is the problem. If you are going to have the pattern 2 letters followed by 18 digits, you can try this regex:

^[\w]{2}[\d]{18}$
Sinan Yaman
  • 5,714
  • 2
  • 15
  • 35
0

Here is the regex for an input like: LT597300010145601329

Try:

[a-zA-Z]{2}\d{18}

Note: If you want only uppercase letters, then: [A-Z]{2}\d{18}

Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38