-
April 29th, 2009, 02:35 AM
#1
Textbox input validation
Hello all,
C# Beginner needs help
I have wrote a small exercise program with 4 text boxes which required user to input only one of the 2 strings: y or n
Could someone kindly show me how to do a simple input validation so only y or n are valid responses
Thank you so much
-
April 29th, 2009, 02:42 AM
#2
Re: Textbox input validation
Sounds like homework. What have you tried already? What was the problem with that code?
-
April 29th, 2009, 03:27 AM
#3
Re: Textbox input validation
Thanks for reply
The following codes get checked by button click
if (((ans1TxBox.Text != y) || (ans1TxBox.Text != n)) ||
((ans2TxBox.Text != y) || (ans2TxBox.Text != n)) ||
((ans3TxBox.Text != y) || (ans3TxBox.Text != n)) ||
((ans4TxBox.Text != y) || (ans4TxBox.Text != n)))
{
MessageBox.Show("Please answer y or n in all 4 textboxes!");
ans1TxBox.Text = null;
ans2TxBox.Text = null;
ans3TxBox.Text = null;
ans4TxBox.Text = null;
ans1TxBox.Focus();
}
The message still pops up event when the input is correct
-
April 29th, 2009, 10:51 AM
#4
Re: Textbox input validation
This looks interesting. Your condition seems to be proper logically, but it fails to validate the string with OR (||) operator.
If you check just for "y" or "n" at a time, it will work. Whereas it will fail to validate if used with OR.
Use regular expressions to solve this problem.
Use the following for regex.
Code:
using System.Text.RegularExpressions;
And the code:
Code:
private void btnCheck_Click(object sender, EventArgs e)
{
if (__isInputProper(textBox1.Text) || __isInputProper(textBox2.Text) ||
__isInputProper(textBox3.Text) || __isInputProper(textBox4.Text))
{
MessageBox.Show("Please answer y or n in all 4 textboxes!");
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
textBox3.Text = string.Empty;
textBox4.Text = string.Empty;
textBox1.Focus();
}
}
private bool __isInputProper(string input)
{
return Regex.IsMatch(input.Trim(), @"[^yn]", RegexOptions.IgnoreCase);
}
Set the max length of the TextBoxes to 1. Then this regex expression will work, Else you have to make another expression to handle other validations.
Last edited by MMH; April 29th, 2009 at 10:53 AM.
Regards,
MMH
Rate my post if you find it usefull.
-
May 1st, 2009, 04:26 AM
#5
Re: Textbox input validation
Originally Posted by MMH
This looks interesting. Your condition seems to be proper logically, but it fails to validate the string with OR (||) operator.
Yes it fails this way it must fail this way. and
Noooooooooo this condition doesn't seem to be logically correct! This condition is per fact totally wrong .
It alsways will answer to 'false Input'. If you have a condition check for 'yes' or 'no' like here, then if you check in this way and you do 'yes' as an input so it is not 'no' at the same time logically !!
And because of that connecting both with 'or' operator || you will always fullfill the condition Putting in 'yes' will have a result true in the 'not no' part and putting in 'no' will result 'true' in the 'not yes' part.
Originally Posted by MMH
Use regular expressions to solve this problem.
And that for a youngster who is just green in programming. Sounds like if you want a baby just has learned his first steps to walk on the Mount Everest
No stay simple. Dont use guns to shot an ant.
Simple use the correct statement Yes / no needs to be connected with AND while the different boxes are connected with OR so we have
Code:
if (((ans1TxBox.Text != y) && (ans1TxBox.Text != n)) ||
((ans2TxBox.Text != y) && (ans2TxBox.Text != n)) ||
((ans3TxBox.Text != y) && (ans3TxBox.Text != n)) ||
((ans4TxBox.Text != y) && (ans4TxBox.Text != n)))
{
MessageBox.Show("Please answer y or n in all 4 textboxes!");
ans1TxBox.Text = string.Empty;
ans2TxBox.Text = string.Empty;
ans3TxBox.Text = string.Empty;;
ans4TxBox.Text = string.Empty;;
ans1TxBox.Focus();
}
If you are talking very precise you will say: If it is not yes AND not no in the first OR the second OR the third OR the forth Textbox then I will write a message.
Note: texts in Textboxes are never null they are string.Empty when they dont contain text
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 2nd, 2009, 07:56 AM
#6
Re: Textbox input validation
Thanks "JonnyPoet" !
i sincerely appreciate that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|