validate form on load

北城以北 提交于 2019-11-28 02:14:36

问题


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');
          }
    }); 

Thanks


回答1:


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

Code:

$(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();
})

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



来源:https://stackoverflow.com/questions/14001028/validate-form-on-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!