3

I'm just getting my feet wet with Jquery Validation Engine and need help on how to write a new regex for disallowing certain characters. I just want to disallow some scripting/coding characters to satisfy a PCI scan requirement on a website.

Per the instructions, I found the translation file where this needs to be added, and I've even located some expressions that are similar. For instance, here's one for allowing only letters and numbers and no spaces:

"onlyLetterNumber": {
  "regex": /^[0-9a-zA-Z]+$/,
  "alertText": "* No special characters allowed"
},

How would I write something up like this, which DISALLOWS only a handful of characters, such as: <>{}[]&$.

tshepang
  • 12,111
  • 21
  • 91
  • 136
dotcominfo
  • 31
  • 1

1 Answers1

1

Try this...

"onlyLetterNumber": {
  "regex": /^[^<>{}[\]&$]+$/,
  "alertText": "* Disallowed character(s) detected"
},

..., which you can preview in a regex fiddle.

The second ^ makes the character group (between [ and ]) a negating character group. At the outset of a character group ^ means to negate characters in the group.

The only tricky part here that I see is having to escape ] (i.e. with \]) to avoid closing the character group.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80