zoukankan      html  css  js  c++  java
  • jQuery-validate error messages in Twitter ...

    http://www.jefclaes.be/2012/11/jquery-validate-error-messages-in.html

    something satisfactory.
    In this example, I have a bootstrapped form that looks like this.

    @using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new { @class = "form-horizontal"})) {
        <div class="control-group">
            <label class="control-label">Old password</label>
            <div class="controls">
                @Html.PasswordFor(m => m.OldPassword)                            
            </div>       
        </div>
        <div class="control-group">
            <label class="control-label">New password</label>
            <div class="controls">
                @Html.PasswordFor(m => m.NewPassword)                
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Confirm password</label>
            <div class="controls">
                @Html.PasswordFor(m => m.ConfirmPassword)                
            </div>                            
        </div>  
        <div class="control-group">
            <div class="controls">
                <button type="submit" class="btn btn-primary">Change password</button>
            </div>
        </div>
    }

    To make the error messages show up in tooltips on the input controls, I eventually ended up with the snippet below.

    $.validator.setDefaults({
        showErrors: function (errorMap, errorList) {
            this.defaultShowErrors();                            
    
            // destroy tooltips on valid elements                              
            $("." + this.settings.validClass)                    
                .tooltip("destroy");            
    
            // add/update tooltips 
            for (var i = 0; i < errorList.length; i++) {
                var error = errorList[i];
                             
                $("#" + error.element.id)
                    .tooltip({ trigger: "focus" })
                    .attr("data-original-title", error.message)                
            }
        }
    });

    I'm setting a custom showErrors function to extend the behaviour of the jQuery validator. I don't want to lose the default behaviour (highlighting etc), so I start with invoking the default showErrors function, to then destroy the tooltip on all valid input elements and to finally add or update the tooltip and its title on all invalid input elements. The errorList argument holds all the information I need for this; an array of invalid elements and their corresponding error messages.

    [Object, Object]
    > 0: Object
    >> element: <input>
    >> message: "The Current password field is required."
    > 1: Object
    >> element: <input>
    >> message: "The New password field is required."
    > length: 2

    The end result looks like this.

  • 相关阅读:
    CxfInvokeUtil
    springboot+webservice(cxf和jax-ws两种方式)
    cxf 工具类转载
    Java动态调用Webservice,不生成客户端,基于soapUI
    转载 CXF动态调用webservice
    spring gzip 静态压缩优化
    sql server2008登录出错怎么整
    配置opencv时计算机显示丢失opencv_world300d.dll如何解决
    随记
    多态与异常处理(课后作业)
  • 原文地址:https://www.cnblogs.com/dotnetmvc/p/3639514.html
Copyright © 2011-2022 走看看