You saw the title and a single number came to mind. If you are English speaking your probably thought ?26.? If your native language is not English you may have thought of other numbers though. Even in European languages there are letters such as � � � ? � �. And that is just the start. So what does this mean to a programmer or a web developer? Well it means you have to be pretty careful if you are writing code to validate that only letters have been entered into a field. I recently read a series of email messages by someone who could not enter their name in a form because it included the letter ?� .? That is a frustrating thing to happen. And honestly asking them to change the ?�? to an ?o? is at best culturally insensitive and more likely outright insulting. So what is a programmer to do?
A typical, old school answer to looking for a non-letter in a string might look something like this Visual Basic code.
Dim t As String = TextBox1.Text.ToUpper()
For i As Integer = 0 To t.Length - 1
If t.Substring(i, 1) < "A" Or t.Substring(i, 1) > "Z" Then
MessageBox.Show("Alphabetic characters required")
Exit For
End If
Next!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->
Looks good but if you test it you will find that all of those non-English characters that are letters are reported as not alphabetic. Not good.
Well often the best way is to rely on system/language functions to do it for you. One has to do their homework though to make sure those functions will do what you want though. I tried this C# code and it didn?t work. You'd probably not expect it to work if you are thinking about international letters because those other letters are outside the range of ?A? to ?Z? in most encodings. If you are not thinking about international letter this may feel right though. A reminder that testing is important.
{
if (t.Substring(i, 1).CompareTo("A") == -1 | t.Substring(i, 1).CompareTo("Z") == 1)
{
MessageBox.Show("Alphabetic characters required");
break;
}
}!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->
Then I tried the IsLetter method in the Char object as follows and it works.
string t = textBox1.Text.ToUpper();
for (int i = 0; i < t.Length; i++)
{
if (!Char.IsLetter(t,i))
{
MessageBox.Show("Alphabetic characters required");
break;
}
}!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->
This works because the IsLetter method understands what ?a letter? is. Reading the documentation was pretty helpful here. This reminds me of one of my favorite things to say to students ? reading the documentation is the shortcut!
There is not typically a lot of room in most high school CS curriculum of course but I think there is room to talk about it in context from time to time. It?s important that students start thinking about this early.
Oh one last tidbit from the recent email conversation I read ? some people don?t have first and last names. They just have one name. Forms that require entering both a first and last name are problematic for them as you can imagine. Solving that problem may be a little more complicated. It?s something to start a conversation about though.
No comments:
Post a Comment