Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate a selected value in a DropDownList?

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>
&nbsp;
<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.

like image 441
Cyberherbalist Avatar asked Mar 05 '15 20:03

Cyberherbalist


People also ask

How do you validate drop down?

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 .

How do you validate a drop down list in HTML?

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.

How do I get the value of the selected dropdown menu item?

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.

How do I get selected text from drop down?

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.


2 Answers

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 the InitialValue 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 the RegularExpressionValidator 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>
&nbsp;
<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 --" />
like image 70
VMAtm Avatar answered Nov 09 '22 23:11

VMAtm


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>
like image 36
Lokesh B R Avatar answered Nov 10 '22 00:11

Lokesh B R