简体   繁体   中英

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)

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 /[az]/gi .

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.

尝试使用此正则表达式:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM