0

I'm using 1000hz Bootstrap Validator to validate fields within my contact form. I'm using data-pattern on an email field to make sure the format is correct.

"data-pattern" => "myemailregexpattern"

I want to write a data-pattern validation where I don't allow email addresses or URLs in a single text field.

I'm programming in Ruby and normally would write logic like the following to validate a text field outside of the form.

if params[:message] =~ /myemailregexpattern/i || params[:message] =~ /myurlregexpattern/
   display error message

Basically I want to do something like the following where it will identify a field value as valid if matches are not found for both regex patterns.

"data-pattern" => "**--negate--**myemailregexpattern**--and---**myurlregexpattern"

OR

"data-pattern" => "**--negate--**((myemailregexpattern)|(myurlregexpattern))"

After reading multiple posts here and on several suggested reference blogs and websites I have not found any examples where an entire regex is negated. I thought I might be able to use a lookaround but it was not clear if it would work or how to write the regex.

  • Is there a reason you can't just negate the comparison? Use `!~` instead of `=~`, or `!( expression )`, etc. – Mark Reed May 25 '17 at 14:01
  • I've seen | for the or condition used. I've seen examples of if-then-else conditions within the regex. I have not seen !~ or ! used in regex expressions. I will try that and see if it works. – Pamela Cook - LightBe Corp May 25 '17 at 14:12
  • They are not regex operators. I'm talking about the outer logic that performs the regex matching. Maybe if you provided more context around what you're trying to do? – Mark Reed May 25 '17 at 14:17
  • I'm trying to replicate my Ruby example using only regex code. The Ruby example will flag an error if an email address or a URL is found in message. I want to replicate that logic using only regex where an error is displayed if an email address or a URL is entered into a text field. I need to enter this logic for data-pattern somehow. The only NOT condition in regex that I know of and that I can find is where you have something like [^abc] where this will exclude a, b and c. All other not conditions are written in a programming language. I may have to write this using JavaScript if possible. – Pamela Cook - LightBe Corp May 25 '17 at 15:36

1 Answers1

0

Part of the problem with "negating" regular expressions is one of definition; exactly what constitutes "not an email address"? The empty string? In a string with no @, does every character match your "not an email address" regular expression? So you still have to have some sort of positive match condition in addition to the negative one.

If your regex engine supports negative lookahead and lookbehind, you can use those to match something that, in addition to whatever it is, is also not something else. Here's an example, if regex is the regular expression you're "negating":

^(?!regex).*(?<!regex)$

That matches anything, as long as whatever matches the .* does not also match regex.

But that's highly engine-dependent, and Javascript (for example) doesn't support lookbehind (positive or negative).

Mark Reed
  • 91,912
  • 16
  • 138
  • 175