In Javascript, is there a way (that survives internationalization) to determine whether a character is a letter or digit? That will correctly identify Ä, ç as letters, and non-English digits (which I am not going to look up as examples)!
In Java, the Character class has some static methods .isLetter(), .isDigit(), .isLetterOrDigit(), for determining in an internationally suitable way that a char is actually a letter or digit. This is better than code like
//this is not right, but common and easy
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) { //it's a letter
because it will pick up non-English letters. I think C# has similar capabilities...
Of course, at worst I can send strings back to the server to be checked but that's a pain...
Of course, in the end I am looking to check if input is a valid name (starts with a letter, the rest is letter or digit). An outside the box possibility for low volume use might be:
var validName=function(atr) {
var ele=document.createElement("div");
try { ele.setAttribute(atr,"xxx"); }
catch(e) { return false; }
return true;
}
This tests out fairly decent in IE, FF and Chrome... Though thorough testing might be needed to figure out how consistent the answers are. And again, not appropriate for heavy duty usage due to element creation.
I have created a small Javascript utility to provide this functionality. I don't claim it is perfect, so let me know how you fair. If people like it, I'll make this the official answer to this question.
CharFunk: https://github.com/joelarson4/CharFunk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With