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?
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?
if (passwordTextBox.Text.Length < 6) { MessageBox.Show("Passwords must be at least 6 characters long."); return /*false*/; } // Do some stuff... return /*true*/; string.Trim() on all text box values, even on passwords, to ensure erroneous white spaces entered by the user are ignored.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.
Use the validating method on your password textbox to enforce length.
if (TextBox1.Text.Length < 6) { MessageBox.Show("password too short"); TextBox1.Focus(); } 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; }