我在单独的javascript文件中编写了客户端自定义验证,将其命名为(mycustomvalidations.js)
这是javascript(mycustomvalidations.js)文件的代码
jQuery.validator.unobtrusive.adapters.add
("selectedvaluewithenteredvaluecheck", ["param"], function (options) {
options.rules["selectedvaluewithenteredvaluecheck"] = options.params.param;
options.messages["selectedvaluewithenteredvaluecheck"] = options.message;
});
jQuery.validator.addMethod("selectedvaluewithenteredvaluecheck",
function (value, element, param) {
console.log(value);
console.log(param);
var UsrEnteredValue = parseInt(param);
var ddlselectedValue = json.stringify(value);
if(ddlselectedValue == 'Amount')
{
if(UsrEnteredValue < 10 || UsrEnteredValue > 20)
{
return false;
}
}
return true;
}
);
这是我的观点..
@Scripts.Render("~/bundles/jquery")// here specifying all script files do i need to
change any thing at here
@model MvcSampleApplication.Models.CrossFieldValidation
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
Html.EnableUnobtrusiveJavaScript(true);
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/mycustomvalidations.js")"
type="text/javascript"></script>
@using (Html.BeginForm("PostValues", "CrossFieldsTxtboxes"))
{
@Html.ValidationSummary(false)
<div class ="editor-field">
@Html.TextBoxFor(m => m.TxtCrossField)
</div>
<div class =".editor-field">
@Html.DropDownListFor(m=> m.SelectedValue , Model.Items)
</div>
<div class=".editor-field">
<input id="PostValues" type="Submit" value="PostValues" />
</div>
}
但我在这一行收到错误
jQuery.validator.unobtrusive.adapters.add
("selectedvaluewithenteredvaluecheck", ["param"], function (options) {
options.rules["selectedvaluewithenteredvaluecheck"] = options.params.param;
options.messages["selectedvaluewithenteredvaluecheck"] = options.message;
});
喜欢这个'jQuery.validator.unobtrusive' is null or not an object'
。
当我尝试在IE8中运行此应用程序但Chrome没有给出任何错误但客户端验证无法在chrome中运行....
任何人都会建议任何关于得到这个错误的想法,我将非常感激..
非常感谢..
修改后的代码:
根据上面指定的链接但仍然出现此错误:
Unhandled exception at line 2, column 1 in localhost:hostnumber/Scripts/mycustomvalidations.js 0x800a138f - Microsoft JScript runtime error: 'jQuery.validator.unobtrusive' is null or not an object
在起跑线
$(function () {
---> jQuery.validator.unobtrusive.adapters.add
("selectedvaluewithenteredvaluecheck", ["param"], function (options) {
options.rules["selectedvaluewithenteredvaluecheck"] = options.params.param;
options.messages["selectedvaluewithenteredvaluecheck"] = options.message;
});
jQuery.validator.addMethod("selectedvaluewithenteredvaluecheck",
function (value, element, param) {
console.log(value);
console.log(param);
if (value != null)
return false;
var UsrEnteredValue = parseInt(param);
var ddlselectedValue = json.stringify(value);
if (ddlselectedValue == 'Amount') {
if (UsrEnteredValue < 10 || UsrEnteredValue > 20) {
return false;
}
}
return true;
});
}(jQuery));
答案 0 :(得分:1)
可能缺少两个javascript引用。
试试这个:
<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>
<script src="@Url.Content("~/Scripts/mycustomvalidations.js")" type="text/javascript"></script>
取代:
<script src="@Url.Content("~/Scripts/mycustomvalidations.js")" type="text/javascript"></script>