Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text box validation in c#

I have a form with a text box that should only accept 4 digit year values between 1980-2011. I'm sure there should be a simple c# validation control to implement this validation check, but I can't seem to find it.

Any suggestions?

like image 393
Meir Avatar asked Feb 21 '11 12:02

Meir


3 Answers

Catch Validating event and add you validation code in there.

For a complete example check MSDN page.

For simple validation you can also use a MaskedTextBox.

like image 73
Adrian Fâciu Avatar answered Oct 17 '22 23:10

Adrian Fâciu


First I'd say, use the max length property set to 4 so that no extra characters can be entered

Beyond that you would have to hook up your own controls to validate it (could be a on text changed validation, on lost focus, etc) that would check that only digits are entered and they are between your specified values

like image 2
Manatherin Avatar answered Oct 17 '22 23:10

Manatherin


A MaskedTextBox would do the trick. Set the mask to your needs: msdn. But I doubt it will check if the value is between a range. It probably only checks if the value is a integer.

like image 1
RvdK Avatar answered Oct 17 '22 21:10

RvdK