How to check selected value in DropDownList?

I want the user to make the right choice in DropDownList , but I do not want to represent any default choice, so the default value -- Select -- . I use RegularExpressionValidator to accept only selected values ​​containing a literal comma (since the actual elements are formatted LastName, FirstName ).

However, I wonder how to get the validator to look for the Selected value! I would prefer NOT to make this confirmation at the server level, and if possible, I would like to keep it as simple as the Regex Validator.

Here is 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> 

Outlying Values:

 -- Select -- Smith, John B Jones, Bill Wilkinson, Harmony Hull, Cordell Hill, Faith 

Is there any way to target the validator on SelectedValue? As shown, the validator does not seem to see any values ​​in the control.

+5
source share
2 answers

I am right that you want to force the user to make a choice, and this choice should fit into your regex ?
If so, you can easily add the RequiredFiledValidator class to your dropdown control, provide InitialValue so that it is equal to your value -- Select -- so that the user is forced to choose something not equal -- Select -- , and this value will be checked using RegularExpressionValidator .

From MSDN:

Validation fails only if the value of the associated input control matches this InitialValue when focus is lost. Lines in both the InitialValue property and the control are entered to remove extra spaces before and after the line before checking.

The check is correct if the input control is empty. If a value is required for the corresponding input control, use the RequiredFieldValidator in addition to the RegularExpressionValidator .

You should also mention the RegularExpressionValidator class, which

Server-side and client-side checks are performed if the browser does not support client-side checks or client-side checks are explicitly disabled (by setting the EnableClientScript property to false).

So your code should look 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 --" /> 
+3
source

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> 
+2
source

Source: https://habr.com/ru/post/1214764/


All Articles