can you use a required field validator for a dropdown list box?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to setup a field validator and tried using the control to validate set to a dropdown list box but did not seem to work. Is there anyway to set this up or do you need to use client side validation? Thanks
 
You can use a required field validator on dropdowns, but you should have a
first item in the dropdown such as --- Choose From Below --- and then you'll
need to add this to your page_load event

dropdown.selectedItem.value = ""

Then the validator won't recognize the first item as a valid item and it
will work.

By the way, all the validators do their validations by client-side
JavaScript that is generated for you (except the CustomValidator). They
also do their checks again server side in case someone tries to maliciously
bypass the client code.


Paul said:
I am trying to setup a field validator and tried using the control to
validate set to a dropdown list box but did not seem to work. Is there
anyway to set this up or do you need to use client side validation? Thanks
 
Hi Scott, thanks for the information. Looks like it is almost working correctly, that is if there is no selection and I hit the submit button I get the validation error message. Next a selection is made and the validation error message goes away. I then hit submit which causes the page load to execute again as well as the Me.DropDownList1.SelectedItem.Value = "" and this causes the validation error message to appear again. So the validation error message seems to appear after the submit button is hit.
Thanks
--
Paul G
Software engineer.


Scott M. said:
You can use a required field validator on dropdowns, but you should have a
first item in the dropdown such as --- Choose From Below --- and then you'll
need to add this to your page_load event

dropdown.selectedItem.value = ""

Then the validator won't recognize the first item as a valid item and it
will work.

By the way, all the validators do their validations by client-side
JavaScript that is generated for you (except the CustomValidator). They
also do their checks again server side in case someone tries to maliciously
bypass the client code.


Paul said:
I am trying to setup a field validator and tried using the control to
validate set to a dropdown list box but did not seem to work. Is there
anyway to set this up or do you need to use client side validation? Thanks
 
Paul,

Two things.

1) To fix the problem of the selected item being fired a second time wrap it
in an if then so it only fires on page load:

If Not Page.IsPostBack Then
'---Select intial item here
End If

2) Always wrap code that should fire only if the page is valid in this if
then:

If Page.IsValid Then
'---Page is valid: run code
End If

If you don't then server side validation isn't checked and anyone who isn't
running javascript will be able to run the post back even with invalid data.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Paul said:
Hi Scott, thanks for the information. Looks like it is almost working
correctly, that is if there is no selection and I hit the submit button I
get the validation error message. Next a selection is made and the
validation error message goes away. I then hit submit which causes the page
load to execute again as well as the Me.DropDownList1.SelectedItem.Value =
"" and this causes the validation error message to appear again. So the
validation error message seems to appear after the submit button is hit.
Thanks
--
Paul G
Software engineer.


Scott M. said:
You can use a required field validator on dropdowns, but you should have a
first item in the dropdown such as --- Choose From Below --- and then you'll
need to add this to your page_load event

dropdown.selectedItem.value = ""

Then the validator won't recognize the first item as a valid item and it
will work.

By the way, all the validators do their validations by client-side
JavaScript that is generated for you (except the CustomValidator). They
also do their checks again server side in case someone tries to maliciously
bypass the client code.


Paul said:
I am trying to setup a field validator and tried using the control to
validate set to a dropdown list box but did not seem to work. Is there
anyway to set this up or do you need to use client side validation? Thanks
 
Hi Justin, thanks for the information. Just wondering what page is valid means, seems like it is needed to protect against those not running java script.
 
Page.IsValid is returns a boolean value indicating if all of the validation
code passed or failed. This is generally used in an If statement:

If Page.IsValid then
'All server validations were successful

Else
'At least one server validation failed

End If

You can also force all the server validation code to run with
Page.Validate().




Paul said:
Hi Justin, thanks for the information. Just wondering what page is valid
means, seems like it is needed to protect against those not running java
script.
 
That's right. The client-side validation controls only work in (recent
versions of) IE, and not even IE users will necessarily have
client-side scripting enabled, so we need to use server-side scripting
to double-check the validity of submitted data, for both useability
and security. The Page.Validate method is automatically called to
accomplish this for us, and we access the results through the
Page.IsValid property.

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuipageclassisvalidtopic.asp
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspplusvalid.asp#aspplusvalid_clientside
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top