Reputation: 1213
I need to validate user input.
All English letters and symbols ".","-","&","'", "&" are allowed. Other symbols aren't allowed.
I have next RegExp which work for string "fffц":
var myRegExp = new RegExp("[a-z]+","ig")
myRegExp.test("fffц") // return false
But it doesn't work with string "fffцfff":
var myRegExp = new RegExp("[a-z]+","ig")
myRegExp.test("fffцfff") // return true, but string contains Russian letters, I expected false
How to write the correct regular expression?
Upvotes: 0
Views: 56
Reputation: 191799
You have [a-z]+
which means "match at least once alphabetic character anywhere." You need to be using anchors
^[a-z.& '-]+$
Upvotes: 1
Reputation: 174836
The below regex would allow only english alphabets,numbers and sepcial characters.
var myRegExp = new RegExp("[ -~]+","ig")
Upvotes: 0