X

Subscribe To Our Mailing List

P.S I will never spam you...100% GUARANTEED!

Monday, December 1, 2014

Invalid range in character set javascript error

Invalid range in character set javascript error

I faced this issue when i was working on IE 8 and this issue has driven me crazy.Since this is not a straight forward issue to fix, i tried to narrow down the problem and finally came to know the regular expression what i used for validation is the culprit for this.This issue would not have come if you are running your application in the high end browsers.

The line which was causing the problem -
var SpecialCharRegex = /^[0-9a-zA-Z',-\s]*$/;
After doing lot of googling i found the issue and the issue is "-" in this regular expression.This error is due to the hyphen, which is mistakenly being used to represent a range and is not properly escaped. To fix this lets add backslash before "-" and change the regular expression like this -
var SpecialCharRegex = /^[0-9a-zA-Z',\-\s]*$/;
Simple logic is either put "-" at the beginning or end of the character class or use backslash to do a regex escape.
It works like a charm !!


4 comments:

  1. This solved my problem

    ReplyDelete
    Replies
    1. Thanks for commenting. I am glad to hear that.

      Delete
  2. I still get that error even if I DO escape the "-" character with backslash.
    If I move the "-" to the beginning.... the problem disappears.
    But why???

    ReplyDelete
    Replies
    1. I still get that error even if I DO escape the "-" character with backslash -
      Abhijith - Can you paste the regex what you are trying to use ?

      If I move the "-" to the beginning.... the problem disappears.
      Abhijith - Correct. If you add it at the beginning it is being treated as "Hyphen" else it is treated as "Range".

      Delete

Comments Section