Need help with a regex pattern for validating form input

  • ozzyosb100
  • Graduate
  • Graduate
  • User avatar
  • Posts: 191
  • Loc: FL

Post 3+ Months Ago

Hello and thanks for taking a look at my question.

I'll try to be short and to the point. I'm making an HTML form to input my Magic The Gathering cards into a database. I'm having trouble coming up with a regex string to validate the input.

Here is the format to be used. Refer to these allowed characters in the info below:
0-9 W U B R G X [ ]

The entries would have several different formats based on what type of card it is:
  • numeric only from 0-16 (ex: 0, 1, 2, 3 etc.)
  • optional leading number, then alpha characters (except "X") only. Different characters may be used at once but should be all grouped together(ex: G, 1G, GG, 2GG, 3GGW, 4WU, WU)
  • X can be by itself, or with a leading number, there may be up to 3 Xs in a row (ex: X, 1X, 2X, 1XX, XX, XXX, 1XXX)
  • The brackets are used to enclose a pair of two unique alpha characters (except "X"), which may repeat several times, but different pairs are not allowed. A grouping may be preceded by a digit. (ex: [GW], 1[GW], [GW][GW], 2[BU]; not allowed: [GW][RW])

Things that apply all across the board:
  • A number may never appear after an alpha character
  • All alpha characters are grouped by kind (allowed: GGW, GGWW, RRWWGG; not allowed: GRG, RRGR)
  • A zero by itself may be entered, but cannot be used in combination with any other character
  • I believe there is never an instance where an alpha character is used, where the preceding number is more than 1 digit.

This is what I've been able to deduce just from knowing the game, however if anyone knows the game and I've missed or have some thing wrong, I'd be glad to know. That said, I'm looking for help in figuring out the regex pattern to use to validate this in an HTML form.

<input name="foo" type="text" id="bar" required pattern=" :?: " title ="foobar">

I would enter the pattern that I've been able to work out so far, but I've mangled it in attempts to get it to work right to the point its nonsense and its best to start fresh.

I've tried to use some tools and sites like but the fault is in my level of understanding of regex syntax it seems, so any assistance would be great and I thank you in advance.

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 42 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.