RegularExpressionValidator With DropDownList(asp.net)
I'm trying to display an error message when someone tries to submit a form without selectiong a "state" from a dropdownlist. I know I can put in an error message using a RegularExpressionValidator, but what would I use for the Validation Expression?
<asp:DropDownList ID="dropState" runat="server">
<asp:ListItem Value="" select="True">Select One</asp:ListItem>
<asp:ListItem Value="">Not in USA</asp:ListItem>
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="AZ">Arizona</asp:ListItem>
<asp:ListItem Value="AR">Arkansas</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CO">Colorado</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="DE">Delaware</asp:ListItem>
<asp:ListItem Value="DC">District of Columbia</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="GA">Georgia</asp:ListItem>
<asp:ListItem Value="HI">Hawaii</asp:ListItem>
<asp:ListItem Value="ID">Idaho</asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
<asp:ListItem Value="KS">Kansas</asp:ListItem>
<asp:ListItem Value="KY">Kentucky</asp:ListItem>
<asp:ListItem Value="LA">Louisianna</asp:ListItem>
<asp:ListItem Value="ME">Maine</asp:ListItem>
<asp:ListItem Value="MD">Maryland</asp:ListItem>
<asp:ListItem Value="MA">Massachusetts</asp:ListItem>
<asp:ListItem Value="MI">Michigan</asp:ListItem>
<asp:ListItem Value="MN">Minnesota</asp:ListItem>
<asp:ListItem Value="MS">Mississippi</asp:ListItem>
<asp:ListItem Value="MO">Missouri</asp:ListItem>
<asp:ListItem Value="MT">Montana</asp:ListItem>
<asp:ListItem Value="NE">Nebraska</asp:ListItem>
<asp:ListItem Value="NV">Nevada</asp:ListItem>
<asp:ListItem Value="NH">New Hampshire</asp:ListItem>
<asp:ListItem Value="NJ">New Jersey</asp:ListItem>
<asp:ListItem Value="NM">New Mexico</asp:ListItem>
<asp:ListItem Value="NY">New York</asp:ListItem>
<asp:ListItem Value="NC">North Carolina</asp:ListItem>
<asp:ListItem Value="ND">North Dakota</asp:ListItem>
<asp:ListItem Value="OH">Ohio</asp:ListItem>
<asp:ListItem Value="OK">Oklahoma</asp:ListItem>
<asp:ListItem Value="OR">Oregon</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="RI">Rhode Island</asp:ListItem>
<asp:ListItem Value="SC">South Carolina</asp:ListItem>
<asp:ListItem Value="SD">South Dakota</asp:ListItem>
<asp:ListItem Value="TN">Tennesee</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem Value="UT">Utah</asp:ListItem>
<asp:ListItem Value="VT">Vermont</asp:ListItem>
<asp:ListItem Value="VA">Virginia</asp:ListItem>
<asp:ListItem Value="WA">Washington</asp:ListItem>
<asp:ListItem Value="WV">West Virginia</asp开发者_JS百科:ListItem>
<asp:ListItem Value="WI">Wisconsin</asp:ListItem>
<asp:ListItem Value="WY">Wyoming</asp:ListItem>
<asp:ListItem Value="AB">Alberta</asp:ListItem>
<asp:ListItem Value="BC">British Columbia</asp:ListItem>
<asp:ListItem Value="MB">Manitoba</asp:ListItem>
<asp:ListItem Value="NB">New Brunswick</asp:ListItem>
<asp:ListItem Value="NL">Newfoundland and Labrador</asp:ListItem>
<asp:ListItem Value="NT">Northwest Territories</asp:ListItem>
<asp:ListItem Value="NS">Nova Scotia</asp:ListItem>
<asp:ListItem Value="NU">Nunavut</asp:ListItem>
<asp:ListItem Value="ON">Ontario</asp:ListItem>
<asp:ListItem Value="PE">Prince Edward Island</asp:ListItem>
<asp:ListItem Value="QC">Quebec</asp:ListItem>
<asp:ListItem Value="SK">Saskatchewan</asp:ListItem>
<asp:ListItem Value="YT">Yukon</asp:ListItem>
</asp:DropDownList>
Add an item with an empty value to the dropdown, which you have:
<asp:ListItem Value="" Selected="true">Select One</asp:ListItem>
And use a required field validator with InitialValue set to empty string, like this:
<asp:RequiredFieldValidator ID="reqState" runat="server" InitialValue="" ControlToValidate="dropState" ErrorMessage="*"></asp:RequiredFieldValidator>
Use a RequiredFieldValidator and set it's InitialValue to String.Empty
:
<asp:RequiredFieldValidator ID="valState" runat="server"
ControlToValidate="dropState"
Display="Dynamic"
ErrorMessage="Please select a state in USA."
InitialValue=""
Text="*">
</asp:RequiredFieldValidator>
If this does not work because the DefaultValue must be set to something different than the default String.Empty(not tested), change the value of your empty ListItem to f.e "-1"
nad the Validator's InitialValue accordingly.
If you are having numerical value then InitialValue="0" or in case of string it should be blank.
int value
InitialValue="0"
string value
InitialValue=""
Full Code
<asp:RequiredFieldValidator ID="rfvState" runat="server" ControlToValidate="dropState" Display="Dynamic" ErrorMessage="*" ToolTip="Please select the state" InitialValue="0"/>
Use the RequiredFieldValidator like this below:
<asp:RequiredFieldValidator ID="rfvState" runat="server" ControlToValidate="dropState" Display="Dynamic" ErrorMessage="Please select the state" InitialValue="-1">*</asp:RequiredFieldValidator>
精彩评论