Skip to content

Instantly share code, notes, and snippets.

@orip
Created June 28, 2010 16:12
Show Gist options
  • Select an option

  • Save orip/456034 to your computer and use it in GitHub Desktop.

Select an option

Save orip/456034 to your computer and use it in GitHub Desktop.

Revisions

  1. orip revised this gist Jun 28, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jQuery.validate.requiredIfVisible.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // jQuery.validate.requiredIfVisible.js
    // Copyright (c) 2010 Ori Peleg, http://orip.org, distributed under the MIT license
    (function($) {
    $.validator.addMethod(
  2. orip revised this gist Jun 28, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jQuery.validate.requiredIfVisible.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Copyright (c) 2010 Ori Peleg, distributed under the MIT license
    // Copyright (c) 2010 Ori Peleg, http://orip.org, distributed under the MIT license
    (function($) {
    $.validator.addMethod(
    "requiredIfVisible",
  3. orip revised this gist Jun 28, 2010. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion jQuery.validate.requiredIfVisible.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Copyright (c) 2010 Ori Peleg, distributed under the MIT license
    (function($) {
    $.validator.addMethod(
    "requiredIfVisible",
    @@ -7,13 +8,14 @@ $.validator.addMethod(
    // inspiration: http://remysharp.com/2008/10/17/jquery-really-visible/
    return e.is(":visible") && e.parents(":not(:visible)").length == 0;
    }

    if (isVisible($(element))) {
    // call the "required" method
    return $.validator.methods.required.call(this, $.trim(element.value), element);
    }

    return true;
    },
    "Required"
    $.validator.messages.required
    );
    })(jQuery);
  4. orip revised this gist Jun 28, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jQuery.validate.requiredIfVisible.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ $.validator.addMethod(
    function(value, element, params) {
    function isVisible(e) {
    // the element and all of its parents must be :visible
    // inspiration: http://remysharp.com/2008/10/17/jquery-really-visible/
    return e.is(":visible") && e.parents(":not(:visible)").length == 0;
    }
    if (isVisible($(element))) {
  5. orip created this gist Jun 28, 2010.
    18 changes: 18 additions & 0 deletions jQuery.validate.requiredIfVisible.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    (function($) {
    $.validator.addMethod(
    "requiredIfVisible",
    function(value, element, params) {
    function isVisible(e) {
    // the element and all of its parents must be :visible
    return e.is(":visible") && e.parents(":not(:visible)").length == 0;
    }
    if (isVisible($(element))) {
    // call the "required" method
    return $.validator.methods.required.call(this, $.trim(element.value), element);
    }

    return true;
    },
    "Required"
    );
    })(jQuery);