Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I get this JavaScript error with jQuery Validate?

I use jquery.unobtrusive and jquery.validate libraries in asp.net mvc 5 project to validate user registration on client side.

on browser I get this error:

Uncaught TypeError: $(...).parents(...).andSelf is not a function
    at Object.parse (jquery.validate.unobtrusive.js:211)
    at HTMLDocument.<anonymous> (jquery.validate.unobtrusive.js:392)
    at mightThrow (jquery-3.1.1.js:3570)
    at process (jquery-3.1.1.js:3638)

I use jquery 3.1.1 in my project.It seem to be versions problems.

Any idea how can I solve it?

like image 816
Michael Avatar asked Jan 29 '17 11:01

Michael


People also ask

What is valid () in jQuery?

valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.

What is jQuery validation engine?

jQuery validation engine is a Javascript plugin aimed at the validation of form fields in the browser (IE 6-8, Chrome, Firefox, Safari, Opera 10). The plugin provides visually appealing prompts that grab user attention on the subject matter.

What is javascript validation plugin?

jQuery Validation is a javascript based plugin that helps you implement a powerful client side validator to your form elements that validate the value ( name, email, address, etc..) your user input when submitting.

Does jQuery validate require a form?

The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.


2 Answers

Uncaught TypeError: $(...).parents(...).andSelf is not a function

Read the jQuery docs.

"I use jquery 3.1.1 in my project."

The .andSelf() method was removed from jQuery version 3

It was an alias for the .addBack() method, which should be used instead.

like image 64
Sparky Avatar answered Sep 17 '22 17:09

Sparky


Like @Sparky said, this is because the Unobtrusive JS files obtained from NuGet (if you create an app using File->New in Visual Studio) uses the deprecated andSelf() function.

To resolve, get the latest versions of

jquery.validate.unobtrusive.js jquery.validate.unobtrusive.min.js

from

https://github.com/aspnet/jquery-validation-unobtrusive/tree/master/dist

and replace the ones in your project. Or Download / Update exiting plugin from nuget.

After Replacing Make sure you clear your browser cache. (Control + F5 if you are using chrome)

like image 30
rams Avatar answered Sep 20 '22 17:09

rams