最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Manual jQuery validation call after AJAX call not working? - Stack Overflow

programmeradmin4浏览0评论

I have a mixed client-side/server-side validation using jQuery validation plugin. The validation happens on both submit and for some fields on change. The form is being submitted via AJAX. There is another validation going on the application just before updating the DB. If the data changes are not stored to the database due to this validation failing I'm returning the result via JSON to the JS method handling the AJAX form submission. Ideally I would raise an error with custom message passed from the backend to JS by something like $.validator.showErrors(obj); as discussed here Unfortunately the validator.showErrors(...) method is not defined in that context:

$(document).ready(function() {
    $('.form').each(function() {
    $(this).submit(function(e) {
        e.preventDefault();
        if ($.submitForm()) {
    (...)
            $.post(url, $.param(context), function(data) {
                if (!data.success) {
                    $.validator.showErrors(...);
                    //$("#basicdetails").validate();
                }
            }, "json");
        }
        (...)

You can also see that I've tried form revalidation which should also trigger appropriate error (although more of an hack that the real solution).

I would like to retain static client-side validation and after ajax submission to be able to raise any errors that might have occurred on the backend.

Thank you for any help you can provide.

Pawel

I have a mixed client-side/server-side validation using jQuery validation plugin. The validation happens on both submit and for some fields on change. The form is being submitted via AJAX. There is another validation going on the application just before updating the DB. If the data changes are not stored to the database due to this validation failing I'm returning the result via JSON to the JS method handling the AJAX form submission. Ideally I would raise an error with custom message passed from the backend to JS by something like $.validator.showErrors(obj); as discussed here Unfortunately the validator.showErrors(...) method is not defined in that context:

$(document).ready(function() {
    $('.form').each(function() {
    $(this).submit(function(e) {
        e.preventDefault();
        if ($.submitForm()) {
    (...)
            $.post(url, $.param(context), function(data) {
                if (!data.success) {
                    $.validator.showErrors(...);
                    //$("#basicdetails").validate();
                }
            }, "json");
        }
        (...)

You can also see that I've tried form revalidation which should also trigger appropriate error (although more of an hack that the real solution).

I would like to retain static client-side validation and after ajax submission to be able to raise any errors that might have occurred on the backend.

Thank you for any help you can provide.

Pawel

Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Oct 1, 2010 at 15:33 PawelPawel 1281 gold badge1 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

.validate() is for setting up validation, to trigger it you should use .valid() like this:

$("#basicdetails").valid();
发布评论

评论列表(0)

  1. 暂无评论