0

I've got a couple of validations on my textarea one of which is no email validation, for certain inputs the browser just freezes.

This is my validation function :

noEmail = function() {
    $.validator.addMethod("noEmail", function(value, element) {
        return this.optional(element) || !(value.match(/((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))([\[\{\|\(]?@[\]\}\|\)]?)((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?/i));
  }, emailValidationFailedMessage);
  }

So for some cases when it freezes the browser, how can I troubleshoot that? And determine the cause?

Or maybe better question how can I make sure that my textfield contains no emails, or at least simpler way so my browser doesn't get stuck?

Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • 3
    Holy , your regex is **470 characters long**. No wonder some browsers choke on it. How can you possibly understand and maintain this? – Frédéric Hamidi May 23 '14 at 14:13
  • @Frédéric Hamidi that's good and correct observation. Found it on some git gist. What is my alternative? – Gandalf StormCrow May 23 '14 at 14:14
  • Well, use a simpler regex. There may be parts in there that can be factorized, but I'm not enough of a regex wizard to be able to easily point them out to you. – Frédéric Hamidi May 23 '14 at 14:16
  • that is the deal, I'm not enough of a regex wizard myself :) – Gandalf StormCrow May 23 '14 at 14:31
  • Let's hope one of the others shows up then (okay, maybe not Saruman). – Frédéric Hamidi May 23 '14 at 14:33
  • hehehe that is what I'm hoping for too. Thanks – Gandalf StormCrow May 23 '14 at 14:36
  • I don't really see why you got that regex monster in your code. you want to check for an email address? http://stackoverflow.com/questions/8112329/jquery-email-regex ? or did I misunderstand you? – gulty May 23 '14 at 14:38
  • 2
    @CaioOliveira Matching *valid* email addresses is **hard**. Your regex fails for `foo+bar@example.com`, `foo@localhost`, `foo@bar.co.uk`. Not really useful here, but [this](http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/) is a nice read. – HamZa May 23 '14 at 15:13
  • 1
    @GandalfStormCrow I can say who ever wrote that regex isn't proficient in it. Maybe it's automated? I can tell this by several things I've observed in the pattern: 1) Escaping when not necessary `[\[\{\|\(]` -> `[\[{|(]` 2) Using `|` (or) all over the place while we could just use the character class `([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])` -> `([-\d._~a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])` and much more. My suggestion? Just try to match for `\S+@\S+`. If you want to get serious you might send the matches to the backend and do a validation – HamZa May 23 '14 at 15:18
  • In php, you could use `filter_var()`. You might also want to try to use regex from [here](http://stackoverflow.com/a/719543) which is according to a certain RFC. My question to you is: why? Do you really need this? It's a pain. I mean even if the email is "correct", you won't know if it exists... – HamZa May 23 '14 at 15:20

0 Answers0