1

A plugin allows me to use regular expressions for data validation like so:

<input type="text" data-validation-regexp="/red/i" data-validation="custom">

However instead of entering the regular expression in it's expected format for what I want to achieve which is /red/i - I have had to simply enter the string red into the attribute like so data-validation-regexp="red" in order to get the validation to function. This only tests for the lower case word 'red' though, whereas I want it to be case insensitive and for the 'i' modifier to be honoured.

This raises the question are there any alternative ways to write (I have endlessly played with this and had little success):

/red/i
user1554264
  • 1,204
  • 3
  • 22
  • 47
  • 1
    As a last resort, you could use `[Rr][Ee][Dd]`... – Siguza Sep 13 '15 at 11:07
  • That works apart from one issue: The input 'redccccc' is valid - only 'red', 'RED' and combinations of lower case/upper case should be valid. – user1554264 Sep 13 '15 at 11:10
  • Ah, `^[Rr][Ee][Dd]$` that would be then. – Siguza Sep 13 '15 at 11:11
  • Thank you very much, please allow me to test this then maybe consider submitting that as an answer? – user1554264 Sep 13 '15 at 11:12
  • @user1554264 You could use `(?i)^red$` if the engine supports it. – Biffen Sep 13 '15 at 11:12
  • 1
    The documentation which you link says that the plugin uses html5 for the regex. Therefore [this](http://stackoverflow.com/a/5525111/2677943) probably answers your question. – swenzel Sep 13 '15 at 11:13
  • You can add your own custom validator as [described here (see *Writing a custom validator*)](https://github.com/victorjonsson/jQuery-Form-Validator). Just name your validator, and use `return /red/i.test(value);`. – Wiktor Stribiżew Sep 13 '15 at 11:44

1 Answers1

1

The plugin you link to doesn't seem to support flags.
Thus, your only option is to "manually" match every character in upper or lower case, like

[Rr][Ee][Dd]

And if you want the RegEx only to match the entire input, add ^...$ around it:

^[Rr][Ee][Dd]$
Siguza
  • 21,155
  • 6
  • 52
  • 89
  • May I please ask how we would finally alter the RegEx to allow for non-breaking spaces? For example if an end user enters ' Red '. Currently the input will be invalid if spaces are entered. – user1554264 Sep 13 '15 at 13:32
  • 1
    `^\s*[Rr][Ee][Dd]\s*$` – Siguza Sep 13 '15 at 13:36
  • Thank you very much - though it is vital I break down this code to increase my understanding. I am aware that 1) `[Rr][Ee][Dd]` matches lower/upper case characters. 2) `^[Rr][Ee][Dd]$` I don't quite understand what this achieves apart from preventing characters other than the ones we have manually set. 3) `^\s*[Rr][Ee][Dd]\s*$` allows for spaces before or after the string. – user1554264 Sep 13 '15 at 18:09
  • [RegEx101](https://regex101.com/) explains the entered RegEx, and [Debuggex](https://www.debuggex.com/) creates a graph for it, if that helps you. – Siguza Sep 13 '15 at 19:07