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 apos开发者_如何学运维trophe and &
will be accepted as special char)
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
.
Try to use this regexp:
/^[a-z\s'&]*$/i
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.
精彩评论