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.
Select the cell in the worksheet where you want the drop-down list. Go to the Data tab on the Ribbon, then click Data Validation .
Validate (Check) HTML Select DropDownList using jQuery Inside the jQuery OnClick event handler, the HTML Select DropDownList object is referenced and if the selected value matches the value of the default item then an error message is displayed using JavaScript alert message box.
The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
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 --" />
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With