验证下拉MVC3中使用jQuery(Validate DropDown using Jquery i

2019-08-19 01:00发布

我有实现简单MVC3应用程序,我想验证控制形式,特别是DIV

//视图模型

public class NewStreamViewModel
    {
        public NewStreamViewModel()
        {
            this.Streams = new List<SelectListItem>();
            this.Subjects = new List<SelectListItem>();
            this.Languages = new List<SelectListItem>();
        }

        [Display(Name = "Language")]
        [Required(ErrorMessage = "{0} is Required")]
        public int? LanguageId {get;set;}


        [Display(Name = "Stream")]
        [Required(ErrorMessage = "{0} is Required")]
        public int? StreamId {get;set;}

        public int[] SubjectIds {get;set;}

        public List<SelectListItem> Languages {get;set;}
        public List<SelectListItem> Streams {get;set;}
        public List<SelectListItem> Subjects {get;set;}

    }

//View.cshtml

<div id="tab1">
@using (Html.BeginForm())
{
     <table style="background: none; width: 100%;">
                    <tr>
                        <td>
                            Language
                        </td>
                        <td>
                            @Html.DropDownListFor(m=>m.LanguageId.Value,Model.Languages)
                            @Html.ValidationMessageFor(m=>m.LanguageId.Value)
                        </td>
                    </tr>
                    <tr>
                        <td>Stream
                        </td>
                        <td>
                            @Html.DropDownListFor(m => m.StreamId.Value, Model.Streams)
                             @Html.ValidationMessageFor(m=>m.StreamId.Value)
                        </td>
                    </tr>
                    <tr>
                        <td>Subjects
                        </td>
                        <td>
                            @Html.ListBoxFor(m => m.SubjectIds, Model.Subjects, new { Style = "width:300px;" })
                        </td>
                    </tr>

                </table>
}
    <input type="button" value="Save" id="addspan" />
</div>  



<script type="text/javascript">
    $(function () {
        var count = 2;
        $('#wrap').tabs();
        $('#addspan').click(function () {
            var $step = $('#tab1');
            var validator = $("form").validate(); // obtain validator
            var anyError = false;
            var selects = $('#tab1').find('select');
            selects.each(function () {
                if (!validator.element(this)) { // validate every input element inside this step
                    anyError = true;
                }

            });
            if (anyError) {
                return false;
            }

            count++;
        });
    });
</script>

我怎么可以在jQuery验证在客户端这些下拉菜单?

Answer 1:

收藏此文件到您的页面(这将允许客户端验证):

<script src="@Url.Content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

呼叫:

$('form').valid()

将返回true如果没有错误, false ,如果他们是和显示误差接近输入。



Answer 2:

您可以手动验证下拉列表如下:

$('#addbtn').click(function(){
var ddlvalue= $("#dropdownid option:selected").val();
if(ddlvalue!='-1')
{    
   //Do your work.
}
else
  alert('Please select product");
});


Answer 3:

  <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $('#<%=btnSubmit.ClientID%>').click(function () {
                if ($('#<%=ddlCity.ClientID%>').val() == 0) {
                    alert('Please select Country')
                    return false;
                   }                              
                if ($('#<%=txtName.ClientID%>').val() == "") {
                    alert(' Name')
                    return false;
                }

            });
        });  

    </script>


文章来源: Validate DropDown using Jquery in MVC3