From 554b789747e36e16f6c2aeab7c8177e9b065b92a Mon Sep 17 00:00:00 2001 From: Thomas Ballinger Date: Mon, 20 Jan 2014 21:30:28 -0500 Subject: [PATCH 1/2] add support for checkbox validation --- example/index.html | 12 ++++++++++++ tinyvalidation.js | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/example/index.html b/example/index.html index 9047b7e..5da1075 100644 --- a/example/index.html +++ b/example/index.html @@ -16,6 +16,9 @@ zipCode: function(zip) { var re = /^\d{5}(-?\d{4})?$/; return re.test(zip) ? true : "Invalid zip code (ex. 12345 or 12345-1234)"; + }, + oneChecked: function(check) { + return $(this).closest('form').find('input[name="'+$(this).attr('name')+'"]:checked').length === 1; } } }; @@ -35,6 +38,10 @@ margin-bottom: 16px; } + input[type="radio"] { + display: inline; + } + ol { list-style-type: none; } @@ -72,6 +79,11 @@ +
  • + + Home + Work +
  • diff --git a/tinyvalidation.js b/tinyvalidation.js index 63ad28a..f90ce23 100644 --- a/tinyvalidation.js +++ b/tinyvalidation.js @@ -80,10 +80,23 @@ } } }; + var validateAllFieldsWithName = function (e) { + var $others = $form.find('input[name="'+$(this).attr('name')+'"]'); + $others.each(function () { + if (!$(this).data('validate')) return; + $(this).trigger({ + type: "tv-radioPropagate" + }); + }); + }; if (options.validateOnBlur) $(this).blur(validateField); if (options.validateOnKeyUp) $(this).keyup(validateField); if ($(this).is(':checkbox')) $(this).change(validateField); + if ($(this).is(':radio')) { + $(this).change(validateAllFieldsWithName); + $(this).bind('tv-radioPropagate', validateField); + } if (options.disableSubmit) { $(this).bind('input', validateField); $(this).trigger('input'); From 2da032c6f002818e621680c0c0defdc46e86e724 Mon Sep 17 00:00:00 2001 From: Thomas Ballinger Date: Wed, 5 Feb 2014 11:50:09 -0500 Subject: [PATCH 2/2] rename others -> radios (review note from Nick) --- tinyvalidation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tinyvalidation.js b/tinyvalidation.js index f90ce23..40e66c5 100644 --- a/tinyvalidation.js +++ b/tinyvalidation.js @@ -81,8 +81,8 @@ } }; var validateAllFieldsWithName = function (e) { - var $others = $form.find('input[name="'+$(this).attr('name')+'"]'); - $others.each(function () { + var $radios = $form.find('input[name="'+$(this).attr('name')+'"]'); + $radios.each(function () { if (!$(this).data('validate')) return; $(this).trigger({ type: "tv-radioPropagate"