简体   繁体   中英

How can I check the validity of a form using jQuery?

I had a previous question about how to check submit it was answered like this:

$("form").on("submit", function() {
    var $form = $(this);

    // if there are validation errors do not continue.
    if (!$form.valid()) {
        return false;
    }
});​

However in VS2012 it shows .valid as "this property does not exist on value of type jQuery". Note that I didn't try to run it yet as I still see the syntax error in VS2012 editor.

In my script directory I have the following _reference.js file. Should I be telling VS2012 about this or referencing it in my script or something?

/// <reference path="jquery-1.8.3.js" />
/// <reference path="jquery.validate.js" />
/// <reference path="jquery.validate.unobtrusive.js" />

Can someone give me some advice on what is wrong?

Solution

I had a _reference.js file but to make this work okay I had to include a reference to the _reference.js file in my script. Now it works okay.

You need to call validate() before calling valid()

$("form").validate();
$("form").on("submit", function() {
    var $form = $(this);

    // if there are validation errors do not continue.
    if (!$form.valid()) {
        return false;
    }
});​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM