Utku Dalmaz
Utku Dalmaz

Reputation: 10172

prevent user to type non-english characters

I am using this one to prevent user to type special chars.

String.prototype.isText = function () {return /^[\w\s]*$/.test(this)}

How can i change it to prevent users to type non-english words ? (only apostrophe and & will be accepted as special char)

Upvotes: 1

Views: 1956

Answers (3)

Stan Rogers
Stan Rogers

Reputation: 2170

Enforcing English with a regular expression? That's a rather naïve approach, and I'm not sure you'd want to include it on your résumé even if you did manage it. I'm not even sure I'd brag about it at the neighborhood café.

Believe it or not, there are a lot of English words (words that have been completely incorporated into the English lexicon) that are properly spelled with characters that don't match /[a-z]/gi.

Upvotes: 4

Nakilon
Nakilon

Reputation: 35084

Try to use this regexp:

/^[a-z\s'&]*$/i

Upvotes: 1

Andrew M
Andrew M

Reputation: 4288

You could try using something like this: http://code.google.com/apis/ajaxlanguage/documentation/#Detect

That is a little bit over complicated if you just want to use regex to detect latin characters, but it will ensure it is actually English.

Upvotes: 1

Related Questions