验证错误列表来自第二次提交/单击表单 [英] validation errorlist comes on second submit / click of form

查看:70
本文介绍了验证错误列表来自第二次提交/单击表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有jquery和jquery验证的ASP.NET MVC.

I am using ASP.NET MVC with jquery and jquery validation.

我创建了一个页面,其中包含一些条目的名称和地址等...

I have created one page that contains some entries name and address and etc...

在提交单击时,我要验证我在视图模型中作为必填字段提供的详细信息.

On submit click I want to validate the details as I have given in view model as required field.

一切正常,验证成功.

我想获取所有必填字段的错误列表.

I want to get all the error list of the required fields.

我得到的是这样的:

var validator = $("#formname").validate();

for (var i = 0; i < validator.errorList.length; i++) {
    console.log(validator.errorList[i].message);
}

在这里一切都运行良好,但是我面临的问题是第二次单击时出现errorList/第一次单击时未提交.

All running fine here as well but the problem I am facing here is I got the errorList on second click / submit not on first click.

我希望所有的errorList都单击/提交.

I want all the errorList on first click / submit.

为什么我不理解第二次点击?

Why it is coming on second click I don't understand?

请完成或解决此问题的人,帮助我解决此问题.

Please who has done this issue or solved it, help me to solve this.

任何帮助将不胜感激.

我的代码示例:

 $('#formname').submit(function () {
    $.validator.unobtrusive.parse($('#formname'));
    var $form1 = $('#workoerderDetails');

//Here I try to get all the error list starts
    var validator = $("#formname").validate();

        for (var i = 0; i < validator.errorList.length; i++) {
            console.log(validator.errorList[i].message);
        }
//Here I try to get all the error list ends

    if ($form1.valid()) {
    //my code if no any validation fire
    }
    else{
    return false;
    }
        });

$ form1运行得非常好,但是我想从中获取所有错误列表,因此我使用了验证器变量,在该变量中,我将所有错误列表都放入了for循环中,但是它使我可以进行第二次提交/单击.

$form1 is running perfectly fine but I want to get all the error list from that so I use validator variable where I get all the errorlist in for loop but it gives me on second submit / click.

推荐答案

我偶然不知道这是什么,但我给出了自己的第五个问题的答案.

This is accidentally I don't know but I am giving my own fifth question's answer.

var settings = $.data($('#formname')[0], 'validator').settings;

             $("#formname").bind("invalid-form.validate", function (form, validator) {
                 var errors = validator.numberOfInvalids();
                 var message = "Please fix these " + errors + " errors." ;
                 if (validator.errorList.length > 0) {
                     for (var x = 0; x < validator.errorList.length; x++) {
                         message += "<br/>\u25CF " + validator.errorList[x].message;
                     }
                     $("#errorList").show();
                     $("#errorList").html("");
                     $("#errorList").html(message);
                 }
                 else {
                     $("#errorList").hide();
                     $("#errorList").html("");
                 }

             });

             settings.submitHandler = function (form) {
                 if (confirm("Are you sure you wish to submit"))
                     form.submit();
             };

感谢这篇很棒的文章代码项目.

感谢所有响应迅速的用户.

Thanks to all the users who has responded very quickly.

这篇关于验证错误列表来自第二次提交/单击表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆