bootstrap-validation.min.js 2.39 KB
/*!  http:/www.newkou.org | 不会飞的羊 */
!function(a){a.fn.validation=function(b){return this.each(function(){e=a.extend({},a.fn.validation.defaults,b),g(this)})},a.fn.validation.defaults={validRules:[{name:"required",validate:function(b){return a.trim(b)==""},defaultMsg:"\u8bf7\u8f93\u5165\u5185\u5bb9\u3002"},{name:"number",validate:function(a){return!/^[0-9]\d*$/.test(a)},defaultMsg:"\u8bf7\u8f93\u5165\u6570\u5b57\u3002"},{name:"mail",validate:function(a){return!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(a)},defaultMsg:"\u8bf7\u8f93\u5165\u90ae\u7bb1\u5730\u5740\u3002"},{name:"char",validate:function(a){return!/^[a-z\_\-A-Z]*$/.test(a)},defaultMsg:"\u8bf7\u8f93\u5165\u82f1\u6587\u5b57\u7b26\u3002"},{name:"chinese",validate:function(a){return!/^[\u4e00-\u9fff]$/.test(a)},defaultMsg:"\u8bf7\u8f93\u5165\u6c49\u5b57\u3002"}]};var b=!1,c=!1,d=!1,e={},f=function(b,d){var f=a(b),g=!1,h="";for(i=0;i<d.length;i++){var k=!0,l=d[i],m=f.attr(l+"-message")==undefined?null:f.attr(l+"-message");l.substr(0,1)=="!"&&(k=!1,l=l.substr(1,l.length-1));var n=e.validRules;for(j=0;j<n.length;j++){var o=n[j];if(l==o.name&&o.validate.call(b,f.val())==k){g=!0,h=m==null?o.defaultMsg:m;break}}if(g)break}var p=f.parents(".controls"),q=f.parents(".control-group"),r=p.children(".help-block, .help-inline");if(g){if(!q.hasClass("error")){if(r.length>0){var s=r.text();p.data("help-message",s),r.text(h)}else p.append('<span class="help-inline">'+h+"</span>");q.addClass("error")}}else if(c){if(r.length>0){var s=p.data("help-message");s==undefined?r.remove():r.text(s)}q.attr("class","control-group")}else if(r.length>0){var s=r.text();p.data("help-message",s)}return!g},g=function(e){a(e).submit(function(){if(b)return!1;b=!0;var e=!1;return a("input, textarea",this).each(function(){var b=a(this),c=b.attr("check-type")==undefined?null:b.attr("check-type").split(" ");c!=null&&c.length>0&&(f(this,c)||(d==0&&(scrollTo(0,b[0].offsetTop-50),d=!0),e=!0))}),d=!1,c=!0,e?(b=!1,a("input, textarea").each(function(){var b=a(this),c=b.attr("check-type")==undefined?null:b.attr("check-type").split(" ");c!=null&&c.length>0&&(b.focus(function(){var a=b.parents(".controls"),c=b.parents(".control-group"),d=a.children(".help-block, .help-inline");if(d.length>0){var e=a.data("help-message");e==undefined?d.remove():d.text(e)}c.attr("class","control-group")}),b.blur(function(){f(this,c)}))}),!1):!0})}}(window.jQuery)