I need to validate alphabetical characters in a text field. What I have now works fine, but there is a catch, I need to allow accented characters (like āēīūčļ) and on a Latvian keyboard these are obtained by typing the singlequote first ('c -> č), so my validator fails is the user types the singlequote and a disallowed character like a number, obtaining '1.
I have this coffeescript-flavor jQuery webpage text entry field validator that only allows alphabetical characters (for entering a name).
allowAlphabeticalEntriesOnly = (target) ->
target.keypress (e) ->
regex = new RegExp("[a-zA-Z]")
str = String.fromCharCode((if not e.charCode then e.which else e.charCode))
return true if regex.test(str)
e.preventDefault()
false
And it gets called with:
allowAlphabeticalEntriesOnly $("#user_name_text")
The code and regex work fine, denying input of most everything except small and large letters and the singlequote, where things get tricky.
Is there a way to allow accented characters with the singlequote layout, but deny entry of forbidden characters after the quote?
EDIT: If all else fails, one can implement back-end invalid character deletion a-la .gsub(/[^a-zA-Z\u00C0-\u017F]/, '')
, which is what I ended up doing