Click here to Skip to main content
13,402,670 members (39,854 online)
Rate this:
Please Sign up or sign in to vote.
I have a question related to good programming practice.

Suppose I need to input a name of my customer in a text box, but the character to be input should only be letters and spaces. The customer can enter numbers, !@, etc, they can make an error unknowingly "arpan1" can be saved into the database.

There are two ways to solve this problem, one is when a user press save button then a message box can be displayed or error defining label can be shown as seen in many website forms, but what if we don't allow user to input numbers and other invalid characters. If the user presses 1 then the 1 or any other invalid character does not appear in the text box by some event handling method.

I would like to know which one is better to do?

Thank you,
Posted 19-Nov-11 22:48pm
Updated 19-Nov-11 23:46pm
SAKryukov 20-Nov-11 19:40pm
Good and important question; I voted 5; please see what I think about the matter.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

There is one simple principle: don't brainwash the user! Every time you show an extra error message to the user saying "What you just did is wrong, because you cannot use A, B and C" can be perceived as a brainwash.

Therefore, preventing such situation is much better. However, not in the way suggested by lion king — never do such things, just imagine the user emotion when she/he tried to type something and got something completely different. Even more reasonable "correct as you type" in text processor is switched off in many cases.

Your first bet would be filtering some useless characters. However, make sure you don't make process of typing more complex than that. Some developers try to make the filtering rules depending on the context, add some text, etc. This would be very confusing.

If the validity rules for the text are very complex, consider combination of filtering and validation. For example, you need to enter positive fractional number. You cannot really keep it valid while typing. Imagine the user typed a decimal separator (a point in English cultures) and later wanted to change its position. Imagine that you try to use a filter which would block an attempt to add more than one separator. A user can first delete one separator and type it again in another position; it will work. But a user could decide to type a second separator first and delete the first one after that. Blocking a second separator can confuse user too much. Therefore, you could just filter everything except digits, separator and possibly 'E' and '-', let the user type it and show the message "Invalid numeric format" or "… must be in the range…". Do it carefully and correctly. An exception handler can even set up a selection on a fragment of text to be fixed, but — never ever more that that! You don't want to be intrusive.

Good luck,
Albert Holguin 20-Nov-11 20:21pm
I don't agree with the brainwashing concept, it depends on the situation. Sometimes it's appropriate to let the user know his input is invalid and why.
SAKryukov 29-Nov-11 2:51am
I must agree. At the same time, you should understand that my "there is one simple principle" is nothing like "do this and only this". In fact, in this post I explain how to come to a well-weighted decision -- look carefully. Don't you think so?

Thank you for this discussion.
amsainju 29-Nov-11 2:29am
yes it must depend on the situation... if a text box should be filled with numeric data then i prefer to not giving right for user to enter alphabet or other character.. i added my question for the justification of my solution..
SAKryukov 29-Nov-11 2:53am
Thank you. Please also see my comment to the comment by Albert above.
amsainju 2-Dec-11 7:02am
yes in case of the albert comment...yes sometime the user have to know what was wrong with their input and providing the example of the valid inputs or inputs that are not acceptable should be shown to the user for the better understanding of the program by the user. thank u
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can do this quite simply: just handle the TextBox.TextChanged event:
char[] illegalCharacters = "!$%@.,".ToCharArray();
private void myTextBox_TextChanged(object sender, EventArgs e)
    TextBox t = sender as TextBox;
    if (t != null)
        string s = t.Text;
        int index = s.IndexOfAny(illegalCharacters);
        bool changed = false;
        int cpos = t.SelectionStart;
        while (index >= 0)
            s = s.Remove(index, 1);
            changed = true;
            index = s.IndexOfAny(illegalCharacters);
        if (changed)
            t.Text = s;
            t.SelectionStart = cpos < s.Length ? cpos : s.Length;
Albert Holguin 20-Nov-11 20:18pm
If you're going to filter the input, alert the user as well... +4
amsainju 29-Nov-11 2:33am
yes thanks
OriginalGriff 21-Nov-11 3:41am
Possibly with a "beep" or similar, but certainly not with a message box - If you aren't looking at the screen because you are entering data from a form, then you can get a very long way before you notice it didn't like the comma you typed at the start of the sheet...
amsainju 2-Dec-11 7:03am
ya inline messaging could be better than messagebox
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

you can do one thing, you can let a user to type what ever they want in the text box. later when before inserting the data into your database, just remove the charecters from the text box text.
amsainju 20-Nov-11 4:00am
will it be a good programming practice?
SAKryukov 20-Nov-11 19:27pm
It would be a very bad programming practice: unexpected, intrusive and confusing transformation of the user input. I would say: don't even play with similar ideas!
Albert Holguin 20-Nov-11 20:17pm
You can't just remove characters and pass the data on, the user has to be aware at all times that you're manipulating his input data.
lion_king032000 20-Nov-11 23:54pm
what i have provided here is the simplest way. best way is to use validators for each text box.
lion_king032000 21-Nov-11 0:02am
programming practice is not only protecting your data, but also good looking and user friendliness. so you can apply any way, link for validation: javascript function or asp validator control or custom validation from code behind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180218.2 | Last Updated 17 Dec 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100