0

I'm trying to validate a input field with jquery and the validate plugin. But my regex keeps failing. The regex works perfectly if I use ajax and run it through a php file. But I dont want to run a ajax request for every character thats written in the input field. Do regex behave diffrently in javascript? Do I have to place the flag another place?

Uncaught SyntaxError: Invalid regular expression: /^\p{L}[\p{L} \'&-]*[\p{L}]$/: Invalid escape

$.validator.addMethod(
  "regex",
  function(value, element, regexp) {
    var re = new RegExp(regexp);
    return this.optional(element) || re.test(value);
  },
);

contact_name: {
  required: true,
  regex: /^\p{L}[\p{L} \'&-]*[\p{L}]$/u
}
  • "Do regex behave diffrently in javascript?" Yes, you will have to write js variant for this regex, because js regex engine doesn't recognize special chars you have used: https://stackoverflow.com/questions/31450128/validation-using-pl-in-javascript-regexp – sinisake Dec 17 '17 at 23:08

1 Answers1

0

Javascript has a different kind of regex. You can verify your expression here. It is a nice web.

I think that you are trying to do this:

^[a-zA-Z][a-zA-Z '&-]*[a-zA-Z]$
Gyntonic
  • 346
  • 2
  • 4
  • 14