2 Replies - 665 Views - Last Post: 05 August 2012 - 05:19 AM Rate Topic: -----

#1 prasad manne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-May 12

textbox validation

Posted 04 August 2012 - 08:13 PM

private void button1_Click(object sender, EventArgs e)
            if (textBox1.Text == "prani" && textBox2.Text.Trim() == "pppp")
                MessageBox.Show("u have succesfully logedin");
                label3.Visible = true;
                label3.Text = "Incorrect username or password please try again";
                //MessageBox.Show("incorrect username or password Please try again");

i tried this to validate the text box......this is a simple login form.
else block is always being executed even if i give the correct password and username.

make me clear with this....tell me the process to validate the text in text box.

private void button1_Click(object sender, EventArgs e)
02	        {
03	            if (textBox1.Text == "prani" && textBox2.Text == "pppp")
04	            {
05	                MessageBox.Show("u have succesfully logedin");
06	            }
07	            else
08	            {
09	                label3.Visible = true;
10	                label3.Text = "Incorrect username or password please try again";
11	                //MessageBox.Show("incorrect username or password Please try again");
12	                textBox1.Clear();
13	                textBox2.Clear();
14	                textBox2.Focus();
15	            }
16	        }

not working even for this code alse

Is This A Good Question/Topic? 0
  • +

Replies To: textbox validation

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 4818
  • View blog
  • Posts: 15,936
  • Joined: 05-May 12

Re: textbox validation

Posted 04 August 2012 - 09:14 PM

Are you sure that textBox1 is for the user name and textBox2 is for the password? I ask because normally on a failed login, and where the UI clears the input fields and resets the focus (which is what you are doing in your else clause), the focus is put back into the user name field if the user name is cleared. You seem to be putting the focus in textBox2.

This is why you should follow thlIn'toq's advice of taking time to name your controls the moment you drop them into the designer.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Freelance developer
  • member icon

Reputation: 6211
  • View blog
  • Posts: 13,713
  • Joined: 02-June 10

Re: textbox validation

Posted 05 August 2012 - 05:19 AM

Skydiver knew I would throw this in. Luckily this is a rare day this week where I have internet. (I'm away on an installation and there isn't internet everywhere I go in Brazil) And he has seen it in many posts before. But if you haven't seen it in other posts, here it is again.

Also, text comparrisons are case sensitive. "frog" != "Frog"

If you simply put a breakpoint on this line
03 if (textBox1.Text == "prani" && textBox2.Text == "pppp")

You can SEE the values in those textboxes and that should tell you why it is failing.

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
  • Debugging video 1: Breakpoints and Local variables
  • Debugging video 2: Advanced breakpoints
  • Debugging tutorial
  • Debugging tips
  • Debugging in detail
  • Great debugging tips
  • It still doesn't work, article
  • Debugging tools survival guide
  • Video: Tips & Tricks to use Visual Studio to the fullest

    Some of my common tips (some may apply more than others to your specific style):
    • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

    • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

    • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
      You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.

    • Guidelines for names
    • Naming guidelines
    • Design guidelines for classes

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readible.

  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1