[英]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.