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.

jQuery-validate error messages in Twitter ...的更多相关文章

  1. Jquery Validate 相关参数

    Jquery Validate 相关参数 //定义中文消息 var cnmsg = { required: “必选字段”, remote: “请修正该字段”, email: “请输入正确格式的电子邮件 ...

  2. JQuery validate验证规则

    //定义中文消息 var cnmsg = { required: “必选字段”, remote: “请修正该字段”, email: “请输入正确格式的电子邮件”, url: “请输入合法的网址”, d ...

  3. jQuery Validate 表单验证 — 用户注册简单应用

    相信很多coder在表单验证这块都是自己写验证规则的,今天我们用jQuery Validate这款前端验证利器来写一个简单的应用. 可以先把我写的这个小demo运行试下,先睹为快.猛戳链接--> ...

  4. jquery validate表单验证插件-推荐

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  5. 表单验证插件之jquery.validate.js

    提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...

  6. jQuery Validate验证框架详解

    转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...

  7. 利用jquery.validate以及bootstrap的tooltip开发气泡式的表单校验组件

    表单校验是页面开发中非常常见的一类需求,相信每个前端开发人员都有这方面的经验.网上有很多成熟的表单校验框架,虽然按照它们默认的设计,用起来没有多大的问题,但是在实际工作中,表单校验有可能有比较复杂的个 ...

  8. jQuery验证控件jquery.validate.js使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  9. jquery.validate.js插件使用

    jQuery验证控件jquery.validate.js使用说明+中文API 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-valid ...

随机推荐

  1. DNS子域委派配置案例[转载]

    最近在研究linux dns 在51上面看见这篇文章,感觉讲的很透彻,随转载,方便以后自己查阅 原文地址:http://www.51osos.com/a/Linux_CentOS_RedHat/Lin ...

  2. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  3. poj3683 Priest John's Busiest Day

    2-SAT. 读入用了黄学长的快速读入,在此膜拜感谢. 把每对时间当作俩个点.如果有交叉代表相互矛盾. 然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解. 如果有解,跑拓扑排序就能找出一 ...

  4. asp.net获取文件夹下的所有文件

    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...

  5. ios 编译openssl支持arm64(转)

    最近在编译支付宝 快捷支付(无线) ios 端的时候发现demo不支持arm64.在网上找了下,看到客服说是openssl的库文件不支持arm64,于是自己编译了支持arm64的库文件,发现还是不行, ...

  6. c& c++ enum

    1.为什么要用enum       写程序时,我们常常需要为某个对象关联一组可选alternative属性.例如,学生的成绩分A,B,C,D等,天气分sunny, cloudy, rainy等等.   ...

  7. Servlet,JDBC,JSONObject三者配和处理客户端请求并返回正确的json数据

    JSON简介 首先我们来理解json(JavaScript Object Notation),如果你熟悉python的字典结构和列表结构,其实json格式是非常容易理解的,当然不熟也不难理解,网上的资 ...

  8. PHP最佳实践(译)

    原文: PHP Best Practices-A short, practical guide for common and confusing PHP tasks 译者:youngsterxyf 最 ...

  9. <八>面向对象分析之UML核心元素之分析类

    一:基本概念        ---->在那大数项目中,分析类是被忽视的一种非常有用的元素.        ---->分析类用于获取系统中主要的“职责簇”,他们代表系统的原型类,是系统必须处 ...

  10. 学习Mongodb(一)

    图片摘录自陈彦铭出品2012.5的<10天掌握MongDB> MongoDB的特点--->面向集合存储,易于存储对象类型的数据--->模式自由--->支持动态查询---& ...