Reputation: 12319
I want the user to make a valid selection in a DropDownList
, but I don't want to present any selection as the default so the default selected value is -- Select --
. I am using a RegularExpressionValidator
to only accept selected values that contain a literal comma (since the valid items are formatted LastName, FirstName
).
However, I am perplexed as to how to get the validator to look for the Selected value! I'd really rather NOT make this a server-level validation, and if possible I'd like to keep it as simple as a Regex Validator.
Here's the code:
<asp:DropDownList ID="ddNames" runat="server">
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegexValidator" runat="server"
ControlToValidate="ddNames" ValidationExpression=".*\,.*"
ErrorMessage="Select a Valid User">
</asp:RegularExpressionValidator>
The dropdown values are:
-- Select --
Smith, John B
Jones, Bill
Wilkinson, Harmony
Hull, Cordell
Hill, Faith
Is there a way to aim the validator at the SelectedValue? As shown, the Validator seems not to be seeing any of the values in the control.
Upvotes: 5
Views: 2533
Reputation: 28355
Am I right that you want to force the user to make a selection, and that selection should fit into your regex
?
If so, you can easily add the RequiredFiledValidator
class to your dropdown
control, provide the InitialValue
property to it equal to your -- Select --
item value so user will be forced to select something not equal to the -- Select --
, and this value will be validated via RegularExpressionValidator
control.
From MSDN:
Validation fails only if the value of the associated input control matches this
InitialValue
upon losing focus. The strings in both theInitialValue
property and the input control are trimmed to remove extra spaces before and after the string before validation is performed.Validation succeeds if the input control is empty. If a value is required for the associated input control, use a
RequiredFieldValidator
control in addition to theRegularExpressionValidator
control.
Also you should note for a RegularExpressionValidator
class, that
Both server-side and client-side validation are performed unless the browser does not support client-side validation or client-side validation is explicitly disabled (by setting the
EnableClientScript
property to false).
So your code should be something like this:
<asp:DropDownList ID="ddNames" runat="server">
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegexValidator" runat="server"
ControlToValidate="ddNames" ValidationExpression=".*\,.*"
ErrorMessage="Select a Valid User" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="ddNames"
InitialValue="-- Select --" />
Upvotes: 4
Reputation: 282
you can use RequiredFieldValidator.
<asp:DropDownList ID="ddNames" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please select" ControlToValidate="ddNames"
InitialValue="-- Select --"></asp:RequiredFieldValidator>
Upvotes: 2