Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms: How to check for minimum amount of characters in textbox in C#?

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?

like image 522
HelpNeeder Avatar asked Dec 05 '11 02:12

HelpNeeder


2 Answers

if (passwordTextBox.Text.Length < 6)
{
    MessageBox.Show("Passwords must be at least 6 characters long.");
    return /*false*/;
}

// Do some stuff...

return /*true*/;
like image 145
rfmodulator Avatar answered Nov 09 '22 17:11

rfmodulator


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.

like image 29
competent_tech Avatar answered Nov 09 '22 16:11

competent_tech