Here is the code as it currently sits, without validation:
<tr><td>Organizer: (select TBD if organizer is not in drop down)</td>
<td><select name="OrganizerID" id="OrganizerID"><option value="">Select</option>
@foreach (var Organizer in Org)
{
<option value="@Organizer.OrganizerID">@Organizer.OAbbreviation</option>
}
</select></td>
</tr>
And here is the code with validation added, which does not work. Any suggestions?
<tr><td>Organizer: (select TBD if organizer is not in drop down)@if(!ModelState.IsValidField("OAbbreviation")){<text>class="error-label"</text>}</td><td><selectname="OrganizerID"id="OrganizerID"><optionvalue=""class="@Validation.ClassFor("OAbbreviation")"@Validation.For("OAbbreviation")>Select</option>@foreach(varOrganizerinOrg){<optionvalue="@Organizer.OrganizerID">@Organizer.OAbbreviation</option>}</select></td>@space@Html.ValidationMessage("OAbbreviation")</tr>
The user can not write in the drop down box, but to choose preselected vaules by your application. So, you should create a drop box with valid vaules, not to make validation on the drop box. After the user submit the form you can check only the selected
value on the server side to see if it's correct. (somebody could try to send a wrong value and to break your program or database).
using javascript or jquery you can check the validation like value is selected or not in your dropdownlist.what you need to do is call the script on button click and in your script find the dropdownlist and can check the value selected or not.
wavemaster
Participant
1295 Points
1131 Posts
how to add validation on a dropdown input field?
Jan 05, 2013 02:07 PM|LINK
Ok, I understand how make validation work on a regular input field. Code sample is below.
The next step is to get validation working on the drop down input fields:
<tr><td>Event Name:@if (!ModelState.IsValidField("formName")) {<text>class="error-label"</text>}</td> <td><input type="text" name="formName" id="formName" value="@Request["formName"]" class="@Validation.ClassFor("formName")" @Validation.For("formName")/>@space @Html.ValidationMessage("formName")</td> </tr>Here is the code as it currently sits, without validation:
<tr><td>Organizer: (select TBD if organizer is not in drop down)</td> <td><select name="OrganizerID" id="OrganizerID"><option value="">Select</option> @foreach (var Organizer in Org) { <option value="@Organizer.OrganizerID">@Organizer.OAbbreviation</option> } </select></td> </tr>And here is the code with validation added, which does not work. Any suggestions?
HostingASPNe...
All-Star
15882 Points
2977 Posts
Re: how to add validation on a dropdown input field?
Jan 05, 2013 02:15 PM|LINK
Hello,
The user can not write in the drop down box, but to choose preselected vaules by your application. So, you should create a drop box with valid vaules, not to make validation on the drop box. After the user submit the form you can check only the selected value on the server side to see if it's correct. (somebody could try to send a wrong value and to break your program or database).
Regards
Free ASP.NET Examples and source code.
wavemaster
Participant
1295 Points
1131 Posts
Re: how to add validation on a dropdown input field?
Jan 05, 2013 02:18 PM|LINK
Can I check to see if a selection was made and how would I do that?
sanjayverma_...
Participant
1476 Points
350 Posts
Re: how to add validation on a dropdown input field?
Jan 05, 2013 04:24 PM|LINK
hi wavemaster,
,
using javascript or jquery you can check the validation like value is selected or not in your dropdownlist.what you need to do is call the script on button click and in your script find the dropdownlist and can check the value selected or not.
Hope this help you ........
sanjayverma_...
Participant
1476 Points
350 Posts
Re: how to add validation on a dropdown input field?
Jan 05, 2013 04:27 PM|LINK
Hi HostingASPNet,
I think we can do this by using combobox of ajax.we can type in dropdownlist and can get/search the value avalable in dropdownlist .
saeed_saedva...
Member
408 Points
74 Posts
Re: how to add validation on a dropdown input field?
Jan 05, 2013 06:50 PM|LINK
<td> <select name="OrganizerID" id="OrganizerID" @Validation.For("OrganizerID") @Validation.ClassFor("OrganizerID")> <option value="">Select</option> @foreach (var Organizer in Org) { <option value="@Organizer.OrganizerID">@Organizer.OAbbreviation</option> } </select> </td>in fact, everything is right in your code. you should just change your validation code place
Saeed Saedvand