I am currently trying to validate my ASP.NET form. I need to be to sure that the user has entered a password of at least 5 characters.
I have done a check to make sure that something is valid using the following code:
else if (Request.Form["txtPassword"] == "")
{}
I am then checking that the characters is not less than 5 by using the following code:
if (Request.Form["txtPassword"].Length < 5)
{}
However, when I run the form and submit it, instead of it displaying the error to the user about the password length, I keep on getting an error from Visual Studio. Before I have tried to submit the form it displays:
Object reference not set to an instance of an object.
This error is only displayed when I am checking the length not if I am checking the String is empty.
Thanks for any help you can provide.
You have a null reference exception. Request.Form[“txtPassword”] returns null. This is essentiall what is happening:
You cannot access the length of a null string. Try using this instead:
The next question, however, is WHY is it null? Perhaps you have inaccurately named the associated form input to something other than “txtPassword”, or perhaps the data is not being sent via POST.