5

I am trying to make sure that user uses at least 6 characters as a password in my program.

I know how to set maximum size by using MaxLength, but how I do this for the minimum length?

2
  • 2
    You talking windows forms, aspx, html, wpf, silverlight? Commented Dec 5, 2011 at 2:31
  • @Terry Donaghe: WinForms. I have changed my tags. Commented Dec 5, 2011 at 2:33

4 Answers 4

3
if (passwordTextBox.Text.Length < 6) { MessageBox.Show("Passwords must be at least 6 characters long."); return /*false*/; } // Do some stuff... return /*true*/; 
Sign up to request clarification or add additional context in comments.

10 Comments

Ususally, I do a string.Trim() on all text box values, even on passwords, to ensure erroneous white spaces entered by the user are ignored.
Surely you need to validate the password beyond the number of characters it contains, != username, etc. I'm just trying to help out with the minimum length problem.
@Uwe Keim: I believe !string.IsNullOrEmpty(txtUserName.Text.Trim()) is even better.
@rfmodulator: which works fine. This is enough for the purpose of my program.
Yes, but a password of 6 spaces would probably be a bad idea, and it will fail my method, anything else (ie. at least one non-whitespace character) passes, and is considered valid. That seems reasonable to me. You will, of course, need to write your own password policy.
|
2

If you are going to have multiple user interfaces where the user can enter their password (web, mobile, windows client, etc) or would provide a service for doing the same (web, wcf, etc), then I think your best option is to catch this type of error at the most common level to all of these platforms.

We generally implement business rules like this in the database (through stored procedures) so that we have one well-known location to check and change these rules.

If you are using a database that doesn't support stored procedures, then you could implement this functionality in the "business layer", or the set of code responsible for performing the business logic for your application.

Comments

2

Use the validating method on your password textbox to enforce length.

 if (TextBox1.Text.Length < 6) { MessageBox.Show("password too short"); TextBox1.Focus(); } 

1 Comment

Can you post an example? I'm not sure how to check for length of a string.
1

Although competent_tech gives you my recommended approach, a primitive solution would be the following:

Drop a label on your form and give it an ErrorText.

Use the following code for your textbox KeyDown event:

protected override void OnLoad(object sender, EventArgs e) { base.OnLoad(sender, e); txtPassword.KeyDown += OnPasswordKeydown; } protected void OnPasswordKeydown(object sender, KeyEventArgs e) { bool isValid = txtPassword.Text.Length < 6; ErrorText.Visible = isValid; AcceptButton.Visible = isValid; } 

9 Comments

I'm not sure how you use this. What's the purpose of KeyDown event?
KeyDown event is raised each time you type a key in the textbox. Just drop your password textbox on your form and double click on it. This will automatically create a KeyDown event handler
Do you mind mentioning what namespace I must add to use this?
None, ErrorText is a label with a Text like: "Password to short" and AcceptButton is the OK button of your form. By setting Visible or Enabled on this button, we ensure that the user cant accept a password that is to short.
Well, I can't make it work. It won't check while I type. I have edited my OP to show how I am trying to do it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.