Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex work in chrome and explorer but not in firefox

Tags:

regex

I am using Jquery to validate my textbox. my particular regex condition is...

/^[A-Za-z.-\s]*$/.. that is alphabets, space, hyphen, and dots.

Issue is it work great and efficiently in Chrome and explorer but firefox gives error for this regex. I checked using firebug. Even it also not work for firefox.

Error: invalid range in character class
like image 265
Rahul Singh Avatar asked Jan 19 '23 14:01

Rahul Singh


2 Answers

To avoid the hyphen having a special meaning (range of characters) you should put it at the end of the character class:

[A-Za-z.\s-]

Alternatively you can escape it:

[A-Za-z.\-\s]
like image 164
Mark Byers Avatar answered Jan 25 '23 23:01

Mark Byers


Escape the hyphen:

/^[A-Za-z.\-\s]*$/

like image 22
aus Avatar answered Jan 26 '23 01:01

aus