我在单独的 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));