├── README ├── bootstrap-validation.js └── bootstrap-validation.min.js /README: -------------------------------------------------------------------------------- 1 | ### 实在对不起、抱歉,这个插件已不在进行维护以及更新,还请关注的朋友们使用其他插件。 2 | -------------------------------------------------------------------------------- /bootstrap-validation.js: -------------------------------------------------------------------------------- 1 | /* ========================================================= 2 | * bootstrap-validation.js 3 | * Original Idea: http:/www.newkou.org (Copyright 2012 Stefan Petre) 4 | * Updated by 不会飞的羊 (https://github.com/FateSheep/Validation-for-Bootstrap) 5 | * ========================================================= 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * ========================================================= */ 19 | !function($) { 20 | $.fn.validation = function(options) { 21 | return this.each(function() { 22 | globalOptions = $.extend({}, $.fn.validation.defaults, options); 23 | validationForm(this) 24 | }); 25 | }; 26 | 27 | $.fn.validation.defaults = { 28 | validRules : [ 29 | {name: 'required', validate: function(value) {return ($.trim(value) == '');}, defaultMsg: '请输入内容。'}, 30 | {name: 'number', validate: function(value) {return (!/^[0-9]\d*$/.test(value));}, defaultMsg: '请输入数字。'}, 31 | {name: 'mail', validate: function(value) {return (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(value));}, defaultMsg: '请输入邮箱地址。'}, 32 | {name: 'char', validate: function(value) {return (!/^[a-z\_\-A-Z]*$/.test(value));}, defaultMsg: '请输入英文字符。'}, 33 | {name: 'chinese', validate: function(value) {return (!/^[\u4e00-\u9fff]$/.test(value));}, defaultMsg: '请输入汉字。'} 34 | ] 35 | }; 36 | 37 | var formState = false, fieldState = false, wFocus = false, globalOptions = {}; 38 | 39 | var validateField = function(field, valid) { // 验证字段 40 | var el = $(field), error = false, errorMsg = ''; 41 | for (i = 0; i < valid.length; i++) { 42 | var x = true, flag = valid[i], msg = (el.attr(flag + '-message')==undefined)?null:el.attr(flag + '-message');; 43 | if (flag.substr(0, 1) == '!') { 44 | x = false; 45 | flag = flag.substr(1, flag.length - 1); 46 | } 47 | 48 | var rules = globalOptions.validRules; 49 | for (j = 0; j < rules.length; j++) { 50 | var rule = rules[j]; 51 | if (flag == rule.name) { 52 | if (rule.validate.call(field, el.val()) == x) { 53 | error = true; 54 | errorMsg = (msg == null)?rule.defaultMsg:msg; 55 | break; 56 | } 57 | } 58 | } 59 | 60 | if (error) {break;} 61 | } 62 | 63 | var controls = el.parents('.controls'), controlGroup = el.parents('.control-group'), errorEl = controls.children('.help-block, .help-inline'); 64 | 65 | if (error) { 66 | if (!controlGroup.hasClass('error')) { 67 | if (errorEl.length > 0) { 68 | var help = errorEl.text(); 69 | controls.data('help-message', help); 70 | errorEl.text(errorMsg); 71 | } else { 72 | controls.append(''+errorMsg+''); 73 | } 74 | controlGroup.addClass('error'); 75 | } 76 | } else { 77 | if (fieldState) { 78 | if (errorEl.length > 0) { 79 | var help = controls.data('help-message'); 80 | if (help == undefined) { 81 | errorEl.remove(); 82 | } else { 83 | errorEl.text(help); 84 | } 85 | } 86 | controlGroup.attr('class','control-group'); 87 | } else { 88 | if (errorEl.length > 0) { 89 | var help = errorEl.text(); 90 | controls.data('help-message', help); 91 | } 92 | } 93 | } 94 | return !error; 95 | }; 96 | 97 | var validationForm = function(obj) { // 表单验证方法 98 | $(obj).submit(function() { // 提交时验证 99 | if (formState) { // 重复提交则返回 100 | return false; 101 | } 102 | formState = true; 103 | var validationError = false; 104 | $('input, textarea', this).each(function () { 105 | var el = $(this), valid = (el.attr('check-type')==undefined)?null:el.attr('check-type').split(' '); 106 | if (valid != null && valid.length > 0) { 107 | if (!validateField(this, valid)) { 108 | if (wFocus == false) { 109 | scrollTo(0, el[0].offsetTop - 50); 110 | wFocus = true; 111 | } 112 | 113 | validationError = true; 114 | } 115 | } 116 | }); 117 | 118 | wFocus = false; 119 | fieldState = true; 120 | 121 | if (validationError) { 122 | formState = false; 123 | 124 | $('input, textarea').each(function() { 125 | var el = $(this), valid = (el.attr('check-type')==undefined)?null:el.attr('check-type').split(' '); 126 | if (valid != null && valid.length > 0) { 127 | el.focus(function() { // 获取焦点时 128 | var controls = el.parents('.controls'), controlGroup = el.parents('.control-group'), errorEl = controls.children('.help-block, .help-inline'); 129 | if (errorEl.length > 0) { 130 | var help = controls.data('help-message'); 131 | if (help == undefined) { 132 | errorEl.remove(); 133 | } else { 134 | errorEl.text(help); 135 | } 136 | } 137 | controlGroup.attr('class','control-group'); 138 | }); 139 | 140 | el.blur(function() { // 失去焦点时 141 | validateField(this, valid); 142 | }); 143 | } 144 | }); 145 | 146 | return false; 147 | } 148 | 149 | return true; 150 | }); 151 | 152 | 153 | }; 154 | }(window.jQuery); 155 | -------------------------------------------------------------------------------- /bootstrap-validation.min.js: -------------------------------------------------------------------------------- 1 | /*! http:/www.newkou.org | 不会飞的羊 */ 2 | !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;i0){var s=r.text();p.data("help-message",s),r.text(h)}else p.append(''+h+"");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) 3 | --------------------------------------------------------------------------------