0

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));
Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
Glory Raj
  • 17,397
  • 27
  • 100
  • 203

1 Answers1

1

Maybe two javascript references are missing.

Try this:

<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>

in place of:

<script src="@Url.Content("~/Scripts/mycustomvalidations.js")" type="text/javascript"></script>
Fabio S
  • 1,124
  • 6
  • 8
  • hmmm i solved it i am using mvc4 in that we can see like this @Scripts.Render("~/bundles/jquery") .. i am thinking that it will add all scripts to view , but its not we need to add references individually to the view .... – Glory Raj Jul 31 '13 at 09:37
  • Check the file `BundleConfig.cs` in `App_Start` folder. Probably, your bundle `"~/bundles/jquery"` only refers to files `"~/Scripts/jquery-[YourVersion].*"`. In this file, you can create your own bundles or editing the existing ones. If my answer solved your problem, please accept it. Thank you. – Fabio S Jul 31 '13 at 09:54