0

I have been given an assignment where I need to check if a Booking Reference is valid. The Booking Reference has to be in the style of "ABC-1234". I need to use PHP to check the first three letters are either "ACT ABQ BDE". This is then followed by the "-". Then I need to check the numbers are in the following sequence, the first digit is either "1" or "2", and the following three digits are in the 0-9 range. This is the code I currently have to check this:

 if (isset($webdata['bookingreference']  ))  {
  if (!preg_match("/^ [ACT] [ABQ] [BDE] - [(1),(2)]{5}[0-9]{6,}$/", $webdata['bookingreference']))  {
    $formerror['bookingreference'] = '<span class="warn" >Not valid on server: Invalid booking reference</span>';
    $valid = FALSE;
  }

However, when I test this, I always get the message it is valid no matter what the sequence is.

Dharman
  • 30,962
  • 25
  • 85
  • 135
S. Lowe
  • 31
  • 4

1 Answers1

6

Here you go.

^(ACT|ABQ|BDE)-(1|2)[0-9]{3}$

^(ACT|ABQ|BDE) Means that it should start with the ACT or ABQ or BDE.

- sign means as is.

(1|2) should follow with one or two.

[0-9]{3}$ means it should be a numeric from 0-9 and {3} it should be exactly three characters and $ this is the end.

You can check it here.

vaso123
  • 12,347
  • 4
  • 34
  • 64