Error for unknown cause: "SyntaxError: invalid range in character class"

6

Situation:

I have a page that contains a <iframe> of another, on this other page, I have a simple HTML element input :

<input type="text" required="" pattern="[a-Z]{2,35}" tabindex="6" name="edtCidade" id="edtCidade">

I'm pretty sure I made sure there is no event type assigned to it or code by changing its properties.

However, every time I type, any key can be a number, it can be a letter, anything, every time I type it generates this error in the console:

  

SyntaxError: invalid range in character class

Example:

If I type "az123" I will have 5 errors of SyntaxError in the console of my browser.

Important:

This error does not cause me any problem, it is insignificant but it is bothering me, and I would like to know the source of it.

Question:

How to resolve this error? Why is it caused? I would like a detailed explanation.

    
asked by anonymous 14.02.2014 / 16:40

3 answers

6

The error is associated with regular expressions, being caused by range a-Z ( a lowercase and Z uppercase) in the pattern attribute.

As the pattern attribute is an element of input included in HTML5 , the expression will be executed even without any plugins or additional events.

To accept uppercase and lowercase letters, use [a-zA-Z] , as there does not seem to be a way to make the case insensitive pattern.

Note : In Chrome this exception does not occur, it just ignores validation. I imagine, then, that you are using Firefox. :)

Demo on JsFiddle

    
14.02.2014 / 17:04
4

It's a regular expression error.

[a-Z]{2,35} não é vlálido  
Use [a-zA-Z]{2,35}

Try using

<input type="text" required="" pattern="[a-zA-Z]{2,35}" tabindex="6" name="edtCidade" id="edtCidade">
    
14.02.2014 / 17:17
4

Lists in regular expressions are based on the ASCII table ( link ). According to this table, a is the character of number 97 and Z is the character of number 90 - because of this, it does not make sense for you to write a list that says "here all characters between 97 and 90 are valid" ( the first number should be less than the second).

The common thing is to do [a-zA-Z0-9_]+ or, simply, \w+ . To validate only numbers use [0-9]+ , or \d+ .

    
14.02.2014 / 17:13