我已经创建了一个自定义验证属性,它在服务器端工作(在发布表单之后),但我无法让验证工作在客户端.
自定义属性是:
public class ReasonableAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
return Approval.IsNumberReasonable(value.ToString());
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "reasonable";
yield return rule;
}
}
Run Code Online (Sandbox Code Playgroud)
该IsNumberReasonable()功能少了点上的字段输入一些检查,使他们进入知道什么可以合理是一个批准文号(如不为空,或"未知"或类似的东西)返回bool true/false.
我的模型然后包含:
[Display(Name = "Approval Number"]
[Reasonable(ErrorMessage = "Please enter a reasonable {0}")]
public String ApprovalNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
并且视图包含:
@Html.LabelFor(model => model.ApprovalNumber, new { @class = "control-label col-md-3" })
<div class="col-md-9 append …Run Code Online (Sandbox Code Playgroud) validation asp.net-mvc jquery unobtrusive-validation asp.net-mvc-4
我正在尝试将 Google Geocode Autocomplete 字段添加到我的表单中。
我已经能够让它限制返回到国家 (AU) 的结果,但如果我也能将结果限制到州,那就更好了。(新南威尔士州/新南威尔士州)
这是我目前的initialize()功能:
function initialize() {
var options = {
types: ['geocode'],
componentRestrictions: {country:"AU"}
};
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}
Run Code Online (Sandbox Code Playgroud)
我已阅读文档并发现您可以使用types参数来限制结果,但我一直无法弄清楚确切的代码应该是什么。
只需将其更改为:
types: ['administrative_area1':'NSW'], // OR 'New South Wales'
Run Code Online (Sandbox Code Playgroud)
并且与上述类似的各种组合(= 而不是:等)都不起作用..
有人能指出我想要实现的实际语法的正确方向吗?
谢谢。
jquery google-maps autocomplete google-maps-api-3 google-geocoder
我有一个jQuery UI Autocomplete,它由第三方Web服务填充.自动完成功能正常,但是当我从列表中选择一个项目时,自动完成输入字段将变为空白.
自动完成功能用于搜索地址,一旦选中,地址就会被拆分为其组件,这些组件将填写表单的其余部分.
所以,我有以下的输入字段ID:
#FullAddress, #AddressLine1,#AddressLine2, #AddressSuburb,#AddressState, #AddressPostcode
与#FullAddress作为自动完成场.
Web服务返回一个对象数组,每个对象包含几乎与上面名称相同的键值对.
最初JS代码看起来像:
$("#FullAddress").autocomplete({
source: "URL",
dataType: "JSONP",
autoFocus: true,
select: function (event, ui) {
$("#FullAddress").val(ui.item['FullAddress']);
$("#AddressLine1").val(ui.item['Line1']);
$("#AddressLine2").val(ui.item['Line2']);
$("#AddressSuburb").val(ui.item['Suburb']);
$("#AddressState").val(ui.item['State']);
$("#AddressPostcode").val(String(ui.item['Postcode']));
}
});
Run Code Online (Sandbox Code Playgroud)
哪个联系了服务器并返回了结果,但它们没有显示在下拉列表中:

单击任何选项会填写所有包含地址数据的字段(您无法看到所选的地址),除此之外 #FullAddress会被清空.即,在上面的图像中,一旦选择了一个,"123 te"就会消失.
我发现以下内容添加到create修复了下拉显示问题的事件中,但没有解决#FullAddress无论发生什么情况该字段被消隐的事实.
$("#FullAddress").autocomplete({
source: "URL",
dataType: "JSONP",
autoFocus: true,
create: function () {
$(this).data('ui-autocomplete')._renderItem = function (ul, item) {
return $("<li>")
.append('<a>' + item.FullAddress + '</a>')
.appendTo(ul);
};
},
select: function (event, ui) {
$("#FullAddress").val(ui.item['FullAddress']);
$("#AddressLine1").val(ui.item['Line1']);
$("#AddressLine2").val(ui.item['Line2']); …Run Code Online (Sandbox Code Playgroud) jquery ×3
asp.net-mvc ×2
autocomplete ×2
google-maps ×1
javascript ×1
jquery-ui ×1
validation ×1