0

I was doing validation which accepts some special characters, dashes, quotes, apostrophe etc.

here is the code:

jQuery.validator.addMethod("cityvalidation", function (value, element) {
    return this.optional(element) || /^[a-zA-Z\u0080-\u024F\s\/\-\)\(\`\.\"\'\U+2019]+$/i.test(jQuery.trim(value));
}, "You Have Typed Unallowed Charactors");

it is working fine unless if somebody copy quotes or apostrophe from MS Word, then the validation gives else error "You Have Typed Unallowed Charactors" as it does not accept quotes and apostrophe from MS Word.

Please suggest if there is any unicode or character for MS Word quotes and apostrophe that can also bee added in the Validation Regex.

UID
  • 4,434
  • 11
  • 45
  • 75

2 Answers2

0

Got the solution, after tons of search and heavy coded solutions which were difficult for me to understand, I tried one silly solution, I just typed the quotes and apostrophe in MS word and then copied from Word and added in my RegEx Code. below is the latest one:

jQuery.validator.addMethod("cityvalidation", function (value, element) {
    //return this.optional(element) || /^[a-zA-Z\s\)\(]+$/i.test(jQuery.trim(value));
    return this.optional(element) || /^[a-zA-Z\u0080-\u024F\s\/\-\)\(\`\.\"\'\‘\’\”\“]+$/i.test(jQuery.trim(value));
}, "You Have Typed Unallowed Charactors");

As of now its not giving any issue, I tested in multiple browsers too and its working Great!

Hope this works for everyone!!!

UID
  • 4,434
  • 11
  • 45
  • 75
0

MS Word uses a different encoding so you need to use the Regex for them.

single quotes and apostrophe

\u2018\u2019\u201A

double quotes

\u201C\u201D\u201E

How to use them from here: https://prcode.blog/2015/10/17/ms-word-special-characters-regex/

Zoe
  • 27,060
  • 21
  • 118
  • 148
Chris Pateman
  • 519
  • 1
  • 5
  • 16