From e277cb0455a5a87bbbcf8592477575764e80ceb8 Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 25 Nov 2013 17:50:11 +0800 Subject: [PATCH 01/25] make validator works in multi-forms with one function call --- validator.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/validator.js b/validator.js index 3d03b4d..3436465 100755 --- a/validator.js +++ b/validator.js @@ -340,8 +340,7 @@ // after: {Function}, // 表单校验之后,只有返回 True 表单才可能被提交 // } $.fn.validator = function(options) { - var $form = this - , options = options || {} + var options = options || {} , identifie = options.identifie || '[required]' , klass = options.error || 'error' , isErrorOnParent = options.isErrorOnParent || false @@ -349,29 +348,33 @@ , before = options.before || function() {return true;} , after = options.after || function() {return true;} , errorCallback = options.errorCallback || function(fields){} - , $items = fields(identifie, $form) - // 防止浏览器默认校验 - novalidate($form); + this.each(function(){ + var $form = $(this) + , $items = fields(identifie, $form) - // 表单项校验 - method && validateFields.call(this, $items, method, klass, isErrorOnParent); + // 防止浏览器默认校验 + novalidate($form); - // 当用户聚焦到某个表单时去除错误提示 - $form.on('focusin', identifie, function(e) { - removeErrorClass.call(this, $(this), 'error unvalid empty', isErrorOnParent); - }) + // 表单项校验 + method && validateFields.call(this, $items, method, klass, isErrorOnParent); + + // 当用户聚焦到某个表单时去除错误提示 + $form.on('focusin', identifie, function(e) { + removeErrorClass.call(this, $(this), 'error unvalid empty', isErrorOnParent); + }) - // 提交校验 - $form.on('submit', function(e){ + // 提交校验 + $form.on('submit', function(e){ - before.call(this, $items); - validateForm.call(this, $items, method, klass, isErrorOnParent); + before.call(this, $items); + validateForm.call(this, $items, method, klass, isErrorOnParent); - // 当指定 options.after 的时候,只有当 after 返回 true 表单才会提交 - return unvalidFields.length ? - (e.preventDefault(), errorCallback.call(this, unvalidFields)) : - (after.call(this, e, $items) && true); + // 当指定 options.after 的时候,只有当 after 返回 true 表单才会提交 + return unvalidFields.length ? + (e.preventDefault(), errorCallback.call(this, unvalidFields)) : + (after.call(this, e, $items) && true); + }) }) } From fa8d7a8dbfe1981343471b05d17aa75cee1ce651 Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 25 Nov 2013 17:52:00 +0800 Subject: [PATCH 02/25] fix typo identifie --- validator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/validator.js b/validator.js index 3436465..40a274c 100755 --- a/validator.js +++ b/validator.js @@ -330,7 +330,7 @@ // 真正的操作逻辑开始,yayayayayayaya! // 用法:$form.validator(options) // 参数:options = { - // identifie: {String}, // 需要校验的表单项,(默认是 `[required]`) + // identifier: {String}, // 需要校验的表单项,(默认是 `[required]`) // klass: {String}, // 校验不通过时错误时添加的 class 名(默认是 `error`) // isErrorOnParent: {Boolean} // 错误出现时 class 放在当前表单项还是(默认是 element 本身) // method: {String | false}, // 触发表单项校验的方法,当是 false 在点 submit 按钮之前不校验(默认是 `blur`) @@ -341,7 +341,7 @@ // } $.fn.validator = function(options) { var options = options || {} - , identifie = options.identifie || '[required]' + , identifier = options.identifier || '[required]' , klass = options.error || 'error' , isErrorOnParent = options.isErrorOnParent || false , method = options.method || 'blur' @@ -351,7 +351,7 @@ this.each(function(){ var $form = $(this) - , $items = fields(identifie, $form) + , $items = fields(identifier, $form) // 防止浏览器默认校验 novalidate($form); @@ -360,7 +360,7 @@ method && validateFields.call(this, $items, method, klass, isErrorOnParent); // 当用户聚焦到某个表单时去除错误提示 - $form.on('focusin', identifie, function(e) { + $form.on('focusin', identifier, function(e) { removeErrorClass.call(this, $(this), 'error unvalid empty', isErrorOnParent); }) From 38e9b1a0f1e9abc849198fccfd5f98509bf69aa3 Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 25 Nov 2013 17:54:51 +0800 Subject: [PATCH 03/25] remove validate form, cause validate funciton has been removed from $.fn --- index.html | 5 ----- 1 file changed, 5 deletions(-) diff --git a/index.html b/index.html index 2ff5f9b..4d5db74 100755 --- a/index.html +++ b/index.html @@ -93,7 +93,6 @@

# form validator

- @@ -115,10 +114,6 @@

# form validator

$('#event').on('after:hello', function(event, element){ //console.log('`after.hello` event trigger on $("#' + element.id + '")'); }) - - $('#validate-form').on('click', function(){ - $(this).closest('form').validate() - }) From 661c2561ac71ecce225d0e89f0ba59cef44d7465 Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 25 Nov 2013 17:57:01 +0800 Subject: [PATCH 04/25] remove meaningless moe comment from codebase --- validator.js | 1 - 1 file changed, 1 deletion(-) diff --git a/validator.js b/validator.js index 40a274c..b74b9f3 100755 --- a/validator.js +++ b/validator.js @@ -327,7 +327,6 @@ return $form.attr('novalidate') || $form.attr('novalidate', 'true') } - // 真正的操作逻辑开始,yayayayayayaya! // 用法:$form.validator(options) // 参数:options = { // identifier: {String}, // 需要校验的表单项,(默认是 `[required]`) From 3401979a91a2ba00eada498906ee1dd837ba1eac Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 25 Nov 2013 18:47:10 +0800 Subject: [PATCH 05/25] prevent other submit events when validated failed --- validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator.js b/validator.js index b74b9f3..24e39b4 100755 --- a/validator.js +++ b/validator.js @@ -371,7 +371,7 @@ // 当指定 options.after 的时候,只有当 after 返回 true 表单才会提交 return unvalidFields.length ? - (e.preventDefault(), errorCallback.call(this, unvalidFields)) : + (e.preventDefault(), e.stopImmediatePropagation(), errorCallback.call(this, unvalidFields)) : (after.call(this, e, $items) && true); }) }) From 07f686274914fab4182b093ae7b25db59c2902bd Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 25 Nov 2013 19:18:50 +0800 Subject: [PATCH 06/25] prevent default when form has two or more submit events bound --- validator.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/validator.js b/validator.js index 24e39b4..0853ae6 100755 --- a/validator.js +++ b/validator.js @@ -369,10 +369,17 @@ before.call(this, $items); validateForm.call(this, $items, method, klass, isErrorOnParent); + // 当有未通过验证的表单项时阻止其他 submit 事件触发 + // 当有两个或以上的 submit 存在时, 阻止当前 submit 事件的默认行为 // 当指定 options.after 的时候,只有当 after 返回 true 表单才会提交 - return unvalidFields.length ? - (e.preventDefault(), e.stopImmediatePropagation(), errorCallback.call(this, unvalidFields)) : - (after.call(this, e, $items) && true); + if (unvalidFields.length) { + e.preventDefault(); + e.stopImmediatePropagation(); + return errorCallback.call(this, unvalidFields); + } else { + if ($._data($form[0], "events").submit.length > 1) e.preventDefault(); + return after.call(this, e, $items) && true; + } }) }) From 5f5ee4bf79f51d94ec3a06769a56a98546d68386 Mon Sep 17 00:00:00 2001 From: Saito Date: Wed, 27 Nov 2013 13:09:32 +0800 Subject: [PATCH 07/25] add test for submit and prevent default action --- index.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.html b/index.html index 4d5db74..d5a7d12 100755 --- a/index.html +++ b/index.html @@ -107,6 +107,11 @@

# form validator

, isErrorOnParent: true }); + $('#form').on('submit', function(event){ + event.preventDefault(); + alert("submitted!"); + }) + $('#event').on('before:hello', function(event, element){ //console.log('`before.hello` event trigger on $("#' + element.id + '")'); }) From c874f692b036f2049a112ba40e651cc783266a98 Mon Sep 17 00:00:00 2001 From: Saito Date: Tue, 20 May 2014 18:41:03 +0800 Subject: [PATCH 08/25] make validator ignore error empty when the element didn't has required attribute --- validator.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/validator.js b/validator.js index 0853ae6..436afcb 100755 --- a/validator.js +++ b/validator.js @@ -245,8 +245,9 @@ // 所有都最先测试是不是 empty,checkbox 是可以有值 // 但通过来说我们更需要的是 checked 的状态 // 暂时去掉 radio/checkbox/linkage/aorb 的 notEmpty 检测 - if(!(/^(?:radio|checkbox)$/.test(type) || aorb) && !patterns['text'](val)) - return validateReturn.call(this, $item, klass, parent, 'empty') + if(!(/^(?:radio|checkbox)$/.test(type) || aorb) && !patterns['text'](val)){ + return !!$item.attr("required") ? validateReturn.call(this, $item, klass, parent, 'empty') : true + } // 二选一验证:有可能为空 if(aorb) return aorbValidate.apply(this, commonArgs); From 5a32d771538fbede3b5df7456192d3f542671240 Mon Sep 17 00:00:00 2001 From: Saito Date: Thu, 22 May 2014 00:49:51 +0800 Subject: [PATCH 09/25] TIL: false means pass --- validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator.js b/validator.js index 436afcb..23f82fe 100755 --- a/validator.js +++ b/validator.js @@ -246,7 +246,7 @@ // 但通过来说我们更需要的是 checked 的状态 // 暂时去掉 radio/checkbox/linkage/aorb 的 notEmpty 检测 if(!(/^(?:radio|checkbox)$/.test(type) || aorb) && !patterns['text'](val)){ - return !!$item.attr("required") ? validateReturn.call(this, $item, klass, parent, 'empty') : true + return !!$item.attr("required") ? validateReturn.call(this, $item, klass, parent, 'empty') : false } // 二选一验证:有可能为空 From a1f6e229a303773623fb0fbe9a25354508db8ab9 Mon Sep 17 00:00:00 2001 From: Saito Date: Mon, 1 Dec 2014 12:52:07 +0800 Subject: [PATCH 10/25] FIX the email bug when the email is a@aaaa or others --- validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator.js b/validator.js index 23f82fe..e7ab5a7 100755 --- a/validator.js +++ b/validator.js @@ -16,7 +16,7 @@ // $item: {}, email: function(text){ - return /^(?:[a-z0-9]+[_\-+.]?)*[a-z0-9]+@(?:([a-z0-9]+-?)*[a-z0-9]+.)+([a-z]{2,})+$/i.test(text); + return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i.test(text); }, // 仅支持 8 种类型的 day From c7995a55ea51d7252229b5dc4f5cd823758acfcd Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Wed, 16 Mar 2016 20:54:55 +0800 Subject: [PATCH 11/25] add validate success status --- validator.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/validator.js b/validator.js index e7ab5a7..652b5bd 100755 --- a/validator.js +++ b/validator.js @@ -203,13 +203,13 @@ // , message: {String} // error message,只有两种值 // } // NOTE: 把 jQuery Object 传到 trigger 方法中作为参数,会变成原生的 DOM Object - if(message === 'unvalid') removeErrorClass($item, klass, parent); + if(message === 'unvalid') (removeErrorClass($item, klass, parent), removeSuccessClass($item, parent)); return /^(?:unvalid|empty)$/.test(message) ? (ret = { $el: addErrorClass.call(this, $item, klass, parent, message) , type: type , error: message }, $item.trigger('after:' + event, $item), ret): - (removeErrorClass.call(this, $item, klass, parent), $item.trigger('after:' + event, $item), false); + (removeErrorClass.call(this, $item, klass, parent), addSuccessClass.call(this, $item, parent), $item.trigger('after:' + event, $item), false); } // 获取待校验的项 @@ -314,7 +314,16 @@ return $item.data('parent') ? $item.closest($item.data('parent')) : parent ? $item.parent() : $item; } + addSuccessClass = function($item, parent) { + return errorElement($item, parent).addClass("success"); + } + + removeSuccessClass = function($item, parent) { + return errorElement($item, parent).removeClass("success"); + } + addErrorClass = function($item, klass, parent, emptyClass){ + removeSuccessClass($item, parent); return errorElement($item, parent).addClass(klass + ' ' + emptyClass); } From 7f4e356f8f3240152e8d05651c3c18cfa516a357 Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Sat, 23 Apr 2016 17:21:10 +0800 Subject: [PATCH 12/25] support custom pattern --- README.md | 12 ++++++++++++ validator.js | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7e21977..40c3ee2 100755 --- a/README.md +++ b/README.md @@ -162,6 +162,18 @@ $('#event').on('after:hello', function(event, element){ ``` +#### 7.自定义pattern +定义了一个全局FormValidator对象,里面有registerPattern和unRegisterPattern两个方法,支持自定义pattern类型,html中的`data-pattern`被用以识别自定义pattern,属性值为string。自定义方法中传入了当前表单项的值以及当前表单项,例: +```html + +``` + +``` javascript + FormValidator.registerPattern("userName", function(val, $el){}) + + FormValidator.unRegisterPattern("userName") +``` + ## 通用约定和代码规范: - 以 2-spaces 作为缩进 diff --git a/validator.js b/validator.js index 652b5bd..f167d25 100755 --- a/validator.js +++ b/validator.js @@ -9,6 +9,16 @@ , validateFields, radios, removeFromUnvalidFields, asyncValidate, getVal , aorbValidate, validateReturn, unvalidFields = [] + window.FormValidator = { + unRegisterPattern: function (name) { + delete patterns[String(name)]; + }, + + registerPattern: function (name, fn) { + patterns[String(name)] = fn; + } + } + // 类型判断 patterns = { @@ -186,7 +196,7 @@ pattern = $item.attr('pattern'); pattern && pattern.replace('\\', '\\\\'); - type = $item.attr('type') || 'text'; + type = $item.data('pattern') || $item.attr('type') || 'text'; // hack ie: 像 select 和 textarea 返回的 type 都为 NODENAME 而非空 type = patterns[type] ? type : 'text'; val = $.trim(getVal($item)); @@ -195,7 +205,7 @@ // HTML5 pattern 支持 message = message ? message : pattern ? ((new RegExp(pattern)).test(val) || 'unvalid') : - patterns[type](val) || 'unvalid'; + patterns[type](val, $item) || 'unvalid'; // 返回的错误对象 = { // $el: {jQuery Element Object} // 当前表单项 @@ -392,7 +402,6 @@ } }) }) - } }(jQuery); From a5ee9bac61805e98639a4efc4fc1c9937a450644 Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Sat, 23 Apr 2016 17:53:26 +0800 Subject: [PATCH 13/25] add minlength validate --- validator.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/validator.js b/validator.js index f167d25..9555725 100755 --- a/validator.js +++ b/validator.js @@ -59,7 +59,7 @@ number: function(text){ var min = +this.$item.attr('min') , max = +this.$item.attr('max') - , result = /^\-?(?:[1-9]\d*|0)(?:[.]\d)?$/.test(text) + , result = /^\-?(?:[1-9]\d*|0)(?:[.]\d+)?$/.test(text) , text = +text , step = +this.$item.attr('step'); @@ -136,14 +136,23 @@ // [type=text] 也会进这项 text: function(text){ + if(!(text = $.trim(text)).length) return; + var max = parseInt(this.$item.attr('maxlength'), 10) - , noEmpty + , min = parseInt(this.$item.attr('minlength'), 10) + , range + + range = function () { + var ret = true + , length = text.length + + if(min) ret = length >= min + if(max) ret = ret && (length <= max) - notEmpty = function(text){ - return !!text.length && !/^\s+$/.test(text) + return ret } - return isNaN(max) ? notEmpty(text) : notEmpty(text) && text.length <= max; + return range() } } @@ -398,7 +407,7 @@ return errorCallback.call(this, unvalidFields); } else { if ($._data($form[0], "events").submit.length > 1) e.preventDefault(); - return after.call(this, e, $items) && true; + return after.call(this, e, $items); } }) }) From 631c914c62fd8334b88cc097f97996c5aaf0d32a Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Sun, 24 Apr 2016 17:48:49 +0800 Subject: [PATCH 14/25] only validate elements in form, removed element will be ignore --- validator.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/validator.js b/validator.js index 9555725..9624bb9 100755 --- a/validator.js +++ b/validator.js @@ -198,7 +198,6 @@ // 验证后的返回值 validateReturn = function($item, klass, parent, message){ - if(!$item) return 'DONT VALIDATE UNEXIST ELEMENT'; var pattern, type, val, ret, event @@ -232,10 +231,15 @@ } // 获取待校验的项 - fields = function(identifie, form) { + fields = function(identifie, form) { return $(identifie, form); } + // 校验校验项是否存在 + isExist = function($item, $form) { + return $form.find($item).length ? true : false; + } + // 获取待校验项的值 getVal = function($item){ return $item.val() || ($item.is('[contenteditable]') ? $item.text() : ''); @@ -299,12 +303,14 @@ } // 校验表单:表单通过时返回 false,不然返回所有出错的对象 - validateForm = function ($fields, method, klass, parent) { + validateForm = function ($fields, method, klass, parent, $form) { if(method && !validateFields.length) return true; unvalidFields = $.map($fields, function(el){ - var field = validate.call(null, $(el), klass, parent); - if(field) return field; + if (isExist(el, $form)) { + var field = validate.call(null, $(el), klass, parent); + if(field) return field; + } }) return validateFields.length ? unvalidFields : false; @@ -396,7 +402,7 @@ $form.on('submit', function(e){ before.call(this, $items); - validateForm.call(this, $items, method, klass, isErrorOnParent); + validateForm.call(this, $items, method, klass, isErrorOnParent, $form); // 当有未通过验证的表单项时阻止其他 submit 事件触发 // 当有两个或以上的 submit 存在时, 阻止当前 submit 事件的默认行为 From 03d8f9f92928c1f0f329f417d05bbdccb6165563 Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Thu, 28 Apr 2016 18:26:20 +0800 Subject: [PATCH 15/25] fix readme for custome pattern --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 40c3ee2..fca33cf 100755 --- a/README.md +++ b/README.md @@ -162,16 +162,20 @@ $('#event').on('after:hello', function(event, element){ ``` -#### 7.自定义pattern -定义了一个全局FormValidator对象,里面有registerPattern和unRegisterPattern两个方法,支持自定义pattern类型,html中的`data-pattern`被用以识别自定义pattern,属性值为string。自定义方法中传入了当前表单项的值以及当前表单项,例: +#### 7. 自定义pattern +定义了一个全局FormValidator对象,里面有registerPattern和unRegisterPattern两个方法,支持自定义pattern类型,html中的`data-pattern`被用以识别自定义pattern,属性值为string。自定义方法中传入了当前表单项的值以及当前表单项,自定义方法应该返回Boolean,例: ```html ``` ``` javascript - FormValidator.registerPattern("userName", function(val, $el){}) + FormValidator.registerPattern("userName", function(val, $el){ + return val > 0 ? true : false + }) FormValidator.unRegisterPattern("userName") + + ``` ## 通用约定和代码规范: From e34cacf15ed92ac9516a74567ad23a6849d4007e Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Fri, 29 Apr 2016 17:43:08 +0800 Subject: [PATCH 16/25] fix data-pattern doc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fca33cf..c12f244 100755 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ $('#event').on('after:hello', function(event, element){ ``` #### 7. 自定义pattern -定义了一个全局FormValidator对象,里面有registerPattern和unRegisterPattern两个方法,支持自定义pattern类型,html中的`data-pattern`被用以识别自定义pattern,属性值为string。自定义方法中传入了当前表单项的值以及当前表单项,自定义方法应该返回Boolean,例: +定义了一个全局 `FormValidator` 对象,里面有 `registerPattern` 和 `unRegisterPattern` 两个方法,支持 **自定义pattern**,html中的`data-pattern`被用以识别自定义`pattern`,属性值为`string`。若需要使用 `data-pattern`,则 `$el.attr('pattern')`应为空。自定义方法中传入了当前表单项的值以及当前表单项,自定义方法应该返回Boolean,例: ```html ``` From 9b0871cfa800a5e3560de9b306f75615a99bdeb9 Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Wed, 7 Sep 2016 11:18:43 +0800 Subject: [PATCH 17/25] fix selector bug and optimize email pattern --- validator.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/validator.js b/validator.js index 9624bb9..f6ee820 100755 --- a/validator.js +++ b/validator.js @@ -25,8 +25,8 @@ // 当前校验的元素,默认没有,在 `validate()` 方法中传入 // $item: {}, - email: function(text){ - return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i.test(text); + email: function (text) { + return /^(?:[a-z0-9]+[_\-+.]+)*[a-z0-9]+@(?:([a-z0-9]+-?)*[a-z0-9]+.)+([a-z]{2,})+$/i.test(text) }, // 仅支持 8 种类型的 day @@ -269,7 +269,7 @@ // 但通过来说我们更需要的是 checked 的状态 // 暂时去掉 radio/checkbox/linkage/aorb 的 notEmpty 检测 if(!(/^(?:radio|checkbox)$/.test(type) || aorb) && !patterns['text'](val)){ - return !!$item.attr("required") ? validateReturn.call(this, $item, klass, parent, 'empty') : false + return validateReturn.call(this, $item, klass, parent, val.length ? 'unvalid' : 'empty') } // 二选一验证:有可能为空 @@ -292,7 +292,7 @@ // 如果有错误,返回的结果是一个对象,传入 validedFields 可提供更快的 `validateForm` var $items = $(this); if (reSpecialType.test(this.type)) { - $items = $('input[type=' + this.type + '][name=' + this.name + ']', + $items = $('input[type="' + this.type + '"][name="' + this.name + '"]', $items.closest('form')); } $items.each(function(){ @@ -376,7 +376,7 @@ $.fn.validator = function(options) { var options = options || {} , identifier = options.identifier || '[required]' - , klass = options.error || 'error' + , klass = options.klass || 'error' , isErrorOnParent = options.isErrorOnParent || false , method = options.method || 'blur' , before = options.before || function() {return true;} From 8dc4bf63d00850a1cc8f9f3920222a0f154dd96e Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Wed, 7 Sep 2016 15:23:35 +0800 Subject: [PATCH 18/25] validator empty if required --- validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator.js b/validator.js index f6ee820..2a5fe00 100755 --- a/validator.js +++ b/validator.js @@ -269,7 +269,7 @@ // 但通过来说我们更需要的是 checked 的状态 // 暂时去掉 radio/checkbox/linkage/aorb 的 notEmpty 检测 if(!(/^(?:radio|checkbox)$/.test(type) || aorb) && !patterns['text'](val)){ - return validateReturn.call(this, $item, klass, parent, val.length ? 'unvalid' : 'empty') + return !!$item.attr("required") ? validateReturn.call(this, $item, klass, parent, val.length ? 'unvalid' : 'empty') : false } // 二选一验证:有可能为空 From a805c7180fdec6ff36ade68549d7104885b766ee Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Wed, 2 Nov 2016 10:56:23 +0800 Subject: [PATCH 19/25] avoid side space validator when use pattern --- validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator.js b/validator.js index 2a5fe00..ce3a484 100755 --- a/validator.js +++ b/validator.js @@ -207,7 +207,7 @@ type = $item.data('pattern') || $item.attr('type') || 'text'; // hack ie: 像 select 和 textarea 返回的 type 都为 NODENAME 而非空 type = patterns[type] ? type : 'text'; - val = $.trim(getVal($item)); + val = getVal($item); event = $item.data('event'); // HTML5 pattern 支持 From d9090fda95ea0e804b295fda8cd92e31bb24a85d Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Wed, 21 Jun 2017 16:48:12 +0800 Subject: [PATCH 20/25] fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c12f244..dbdb5f0 100755 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ $('#form_id').validator(options); ```js options = { // 需要校验的表单项,(默认是 `[required]`),支持任何 jQuery 选择器可以选择的标识 - identifie: {String}, + identifier: {String}, // 校验不通过时错误时添加的 class 名(默认是 `error`),当校验为空时,还同时拥有 `empty` 这个 classname klass: {String}, From 6a9ca84d789bbcd813b7bc1848bfaee388a8a708 Mon Sep 17 00:00:00 2001 From: ruoqianfengshao Date: Wed, 27 Sep 2017 14:29:44 +0800 Subject: [PATCH 21/25] validate item with delegate and add new param in validator --- index.html | 16 ++++++++++++++- validator.js | 55 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/index.html b/index.html index d5a7d12..6ebc9d3 100755 --- a/index.html +++ b/index.html @@ -91,10 +91,18 @@

# form validator

123
+
+

+ +

+ - +

+ +

+