简体   繁体   English

针对 ASP.Net-MVC3 中的 object 的客户端验证?

[英]Client-side validation against an object in ASP.Net-MVC3?

I have ah HTML5 form with an action defined as follows:我有啊 HTML5 表格,动作定义如下:

@using (Html.BeginForm("SearchAction", "ResultsController"))

The form takes in two text fields:该表单包含两个文本字段:

<input type="text" name="txtSearchTerm" id="txtSearchTerm" class="frontPageInput" placeholder="Begin your search..." required />          
<input type="text" name="txtGeoLocation" id="txtGeoLocation"  class="frontPageInput" required />          

The txtGeoLocation field is an autocomplete field that is fed from a cached object, fed through the controller and by a model repository class through the following jQuery code: The txtGeoLocation field is an autocomplete field that is fed from a cached object, fed through the controller and by a model repository class through the following jQuery code:

<script type="text/javascript" language="javascript">
    $(function () {
        $("#txtGeoLocation").autocomplete(txtGeoLocation, {
            source: function (request, response) {
                $.ajax({
                    url: "/home/FindLocations", type: "POST",
                    dataType: "json",
                    selectFirst: true,
                    autoFill: true,
                    mustMatch: true,
                    data: { searchText: request.term, maxResults: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
                    : "Nothing selected, input was " + this.value);
                document.getElementById("hidLocation").value = ui.item.id;
            }
        });

    });

There's an alert there for debugging.那里有一个用于调试的警报。 When clicking on the text that drops down, this alert fires, however it does not fire if you type in the whole word and hit submit.单击下拉的文本时,会触发此警报,但是如果您输入整个单词并点击提交,它不会触发。

I would like to first, validate the text in the geo text box on the client side, to ensure that it is a value contained in the collection, of not, have the text box in red, communicate that.我想首先在客户端验证地理文本框中的文本,以确保它是集合中包含的值,而不是,让文本框为红色,传达这一点。

Thanks.谢谢。

You can use jquery remote validation using the [Remote()] attribute to validate the value is in the list.您可以使用 [Remote()] 属性使用 jquery 远程验证来验证值是否在列表中。 You will have to do the same check on the server side when you post back as well.当您回发时,您也必须在服务器端进行相同的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP,NET -MVC5客户端验证不起作用 - ASP,NET -MVC5 client-side validation not working 使用MvcContrib FluentHtml的ASP.NET MVC客户端验证 - ASP.NET MVC Client-side validation with MvcContrib FluentHtml ASP.NET MVC客户端验证的自定义属性 - ASP.NET MVC client-side validation of custom attribute JQuery UI 选项卡、ASP .NET MVC 2 和客户端验证 - JQuery UI tabs, ASP .NET MVC 2 and client-side validation ASP.NET MVC 2客户端验证触发错误 - ASP.NET MVC 2 client-side validation triggering incorrectly 在MVC4中针对ViewModel执行客户端验证 - Performing client-side validation in MVC4 View against ViewModel 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? 自定义验证属性中的客户端验证 - asp.net mvc 4 - client-side validation in custom validation attribute - asp.net mvc 4 在 ASP.NET Core MVC 中不工作的 bool 字段的不显眼的客户端验证 - Unobtrusive client-side validation for bool field not working in ASP.NET Core MVC 如何使用客户端验证来处理 c# 中的 asp.net mvc? - How to use client-side validation to work on your asp.net mvc in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM