验证加载表格 [英] validate form on load

查看:68
本文介绍了验证加载表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在此链接中实施相同的验证
http://alittlecode.com/files/jQuery-Validate-Demo/ 因此,如果成功,我会在元素旁边显示复选标记,是否有一种方法可以在默认情况下具有有效输入时在表单加载时显示复选标记,并且仅在提交时显示错误? 这是代码

I'm implementing the same validation in this link
http://alittlecode.com/files/jQuery-Validate-Demo/ So I display check marks next to elements on success, is there a way to display the checkmarks on form load when it has valid input by default, and display errors only on submit ? here is the code

 $("#frmsubmit").validate({
        rules: {
            citstyle: "required",
            spelling: "required",
            uploadfile: {required: function (element) {
                 if($("#filesent").is(':checked'))
                 {
                   return false;  
                 } 
                     else
                     {
                        return true;
                     } 

              }  ,
                        filesize: 8388608},
            wordsno: "required",
            cellphone: "required",
            country: "required",
            address: "required",
            paytype: "required"
        },


        highlight: function(label) {
            $(label).closest('.control-group').addClass('error');
          },
          success: function(label) {
            label
              .text('OK!').addClass('valid')
              .closest('.control-group').addClass('success');
          }
    }); 

谢谢

推荐答案

下面是一个有效的示例: http://jsfiddle.net/Gajotres/TthG9/.我的示例是从链接中获取的代码示例创建的.

Here's an working example: http://jsfiddle.net/Gajotres/TthG9/. My example was created from a code example taken from your link.

代码:

$(document).ready(function(){
    $('#contact-form').validate(
    {
        rules: {
            name: {
                minlength: 2,
                required: true
            },
            email: {
                required: true,
                email: true
            },
            subject: {
                minlength: 2,
                required: true
            },
            message: {
                minlength: 2,
                required: true
            }
        },
        highlight: function(label) {
            $(label).parent().find('.valid').each(function(){
                $('label[class^="valid"]').remove();       
            });
        },        
        success: function(label) {
            if(label.text('OK!').parent().find('.valid').html() == null) {
                label.removeClass('error').addClass('valid');  
            } else {
                label.remove();
            }
        }        
    });
    $("#contact-form").validate().form();
    $('label[class^="error"]:not(.valid)').remove();
})

方法 .form()可用于以编程方式触发表单验证

Method .form() can be used to trigger a form validation programmatically

这篇关于验证加载表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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