-2

"For a reference to be valid, the three letter group must be one of ‘ACT’, ‘ABQ’, ‘ ‘BDE’. Anything else is invalid."

My code is below but it doesn't seem to be working as intended. Could somebody please point me in the right direction. Thanks.

if (isset($webdata['bookingreference'] )) {
    if (!preg_match("[ABQ,ACT,BDE]{1}[-]{1}[1-2]{1}[0-9]{3}", $webdata['bookingreference'])) {
        $formerror['bookingreference'] = '<span class="warn" >Not valid on server: The ref must start with ABQ, ACT or BDE, followed by a hyphen, then either the numbers 1 or 2 followed by two more numbers 0-9';
        $valid = FALSE;
Spum0n1
  • 11
  • 1
  • 3
  • 2
    There's no SQL here. – Barmar Jan 29 '20 at 18:52
  • _In what way_ is it not working? What' _is_ happening? What is the value of `$webdata['bookingreference']`? – Patrick Q Jan 29 '20 at 18:52
  • Sorry php! I'm reading things and typing and wrote the wrong thing. – Spum0n1 Jan 29 '20 at 18:53
  • You need to read a Regular Expression tutorial. You don't understand what `[ABQ,ACT,BDE]` means. Also, `preg_match()` requires you to put delimiters around the regexp. – Barmar Jan 29 '20 at 18:53
  • It should be only allowing any of those 3 letter combinations, followed by a hyphen, then followed by a 1 or a 2 and then any 3 numbers. The form is allowing any combination I enter though, and also constantly echoing the error message. – Spum0n1 Jan 29 '20 at 18:53
  • What you probably want is `(ABQ|ACT|BDE)`. You also never need `{1}`, since regular expressions only match one time unless you quantify them. – Barmar Jan 29 '20 at 18:54
  • `[]` enclose character classes. So `[ABQ,ACT,BDE]{1}` matches any of the characters "A", "B", "Q", ",", "C", "T", "D" or "E" one time. Maybe you want alternates like `(?:ABC|ACT|BDE)`. – sticky bit Jan 29 '20 at 18:55

1 Answers1

1

It should be:

    if (!preg_match("(ABQ|ACT|BDE)-[1-2][0-9]{3}", $webdata['bookingreference'])) {

To make a group you put it inside (), not [], which is for character sets. Alternatives are separated with |, not ,.

There's no need to put - inside [] to match it literally. And there's no need for {1} quantifiers, since that's the default.

Barmar
  • 741,623
  • 53
  • 500
  • 612