I have written client side custom validation in separate javascript file named it as (mycustomvalidations.js)
and this is the code for javascript(mycustomvalidations.js) file
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;
}
);
and this is my view ..
@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>
}
but I am getting error at this line
jQuery.validator.unobtrusive.adapters.add
("selectedvaluewithenteredvaluecheck", ["param"], function (options) {
options.rules["selectedvaluewithenteredvaluecheck"] = options.params.param;
options.messages["selectedvaluewithenteredvaluecheck"] = options.message;
});
like this 'jQuery.validator.unobtrusive' is null or not an object'
.
when I try to run this application in IE8 but chrome does not giving any errors but client side validation does not working in chrome ....
would any one pls suggest any ideas on getting this error that will be very grateful for me ..
Many thanks..
Modified Code :
According to the link specified in above but still getting this error:
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
at the starting line
$(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));