From 8da127704258d69ca7d942c557c4e88bd1d66e35 Mon Sep 17 00:00:00 2001 From: Craig Davis Date: Wed, 3 Apr 2013 10:02:27 -0500 Subject: [PATCH] Escape the name when looking for required radio buttons. Fixes Issue #17 * Return unit testing to default build * Add unit test for Issue #17 --- Gruntfile.js | 4 ++-- dist/uni-form-validation.jquery.js | 15 ++++++++++----- dist/uni-form-validation.jquery.min.js | 4 ++-- dist/uni-form.jquery.js | 2 +- dist/uni-form.jquery.min.js | 2 +- src/uni-form-validation.jquery.js | 4 ++-- src/validators.js | 9 +++++++-- test/issues.js | 20 ++++++++++++++++++++ 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 03d988f..75ec8d1 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -91,7 +91,7 @@ module.exports = function (grunt) { }, src: { files: '<%= jshint.src.src %>', - tasks: ['jshint:src', 'concat', 'uglify'] //, 'qunit'] + tasks: ['jshint:src', 'concat', 'uglify', 'qunit'] }, test: { files: '<%= jshint.test.src %>', @@ -101,7 +101,7 @@ module.exports = function (grunt) { }); // Default task. - grunt.registerTask('default', ['jshint',/*'qunit', */ 'clean', 'concat', 'cssmin', 'uglify']); + grunt.registerTask('default', ['jshint','qunit', 'clean', 'concat', 'cssmin', 'uglify']); // Alias this to make it standard across my repos. (Jasmine v. QUnit) grunt.registerTask('test', ['qunit']); diff --git a/dist/uni-form-validation.jquery.js b/dist/uni-form-validation.jquery.js index 9475040..39de54b 100644 --- a/dist/uni-form-validation.jquery.js +++ b/dist/uni-form-validation.jquery.js @@ -1,4 +1,4 @@ -/*! Uni-Form - v1.5.0 - 2013-03-31 +/*! Uni-Form - v1.5.0 - 2013-04-03 * http://sprawsm.com/uni-form/ * Copyright (c) 2013 Dragan Babic; Licensed MIT */ // @@ -175,9 +175,9 @@ // Set the form focus class and remove any classes other than the focus // class and then hide the default label text - $form.find(options.field_selector).on('focus', function (e) { + $form.find(options.field_selector).on('focus', function () { var $input = $(this); -window.console.log(e); + $form // Remove any other focus highlighting .find('.' + options.focused_class) .removeClass(options.focused_class); @@ -405,23 +405,28 @@ window.console.log(e); (function ($) { + var escapeStr; + $.uniform = $.uniform || {}; // Hold a collection of the various form validators $.uniform.validators = {}; + escapeStr = function (str) { + return (str) ? str.replace(/([ #;&,.+*~\':"!\^$\[\]()=>|\/@])/g,'\\$1'): str; + }; + // Value of field is not empty, whitespace will be counted as empty $.uniform.validators.required = function ($field, caption) { var name; if ($field.is(':radio')) { - name = $field.attr('name'); + name = escapeStr($field.attr('name')); if ($("input[name=" + name + "]:checked").length) { return true; } return $.uniform.i18n('req_radio', caption); } if ($field.is(':checkbox')) { - name = $field.attr('name'); if ($field.is(":checked")) { return true; } return $.uniform.i18n('req_checkbox', caption); } diff --git a/dist/uni-form-validation.jquery.min.js b/dist/uni-form-validation.jquery.min.js index 2b773c8..c2b27d3 100644 --- a/dist/uni-form-validation.jquery.min.js +++ b/dist/uni-form-validation.jquery.min.js @@ -1,4 +1,4 @@ -/*! Uni-Form - v1.5.0 - 2013-03-31 +/*! Uni-Form - v1.5.0 - 2013-04-03 * http://sprawsm.com/uni-form/ * Copyright (c) 2013 Dragan Babic; Licensed MIT */ -(function(a){a.uniform=function(){},a.uniform.get_val=function(a,e,t){var l,n=t;for(e=e.split(" "),l=0;e.length>l;l+=1)if(e[l]===a&&"undefined"!==e[l+1]&&"val-"===e[l+1].substr(0,4))return n=parseInt(e[l+1].substr(4),10);return n},a.uniform.get_label_text=function(e,t){var l=e.closest("label").text();return t=t||a.uniform.defaultOptions,""===l&&(l=e.closest("div."+t.holder_class).find("label").text()),l.replace("*","").replace(":","")},a.fn.uniform=function(e){return e=a.extend(a.uniform.defaultOptions,e),this.each(function(){var t,l,n,i,r,o;return t=a(this),l=[],n=a.proxy(a.uniform.get_label_text,this),i=a.proxy(a.uniform.get_val,this),r=a.proxy(a.uniform.validate,this),t.find(e.field_selector).each(function(){var t=a(this),l=t.val();t.data("default-color",t.css("color")),l!==t.data("default-value")&&l||(t.not("select").css("color",e.default_value_color),t.val(t.attr("data-default-value")))}),(e.ask_on_leave||t.hasClass("askOnLeave"))&&(o=t.serialize(),a(window).bind("beforeunload",function(){return o!==t.serialize()&&(e.ask_on_leave||t.hasClass("askOnLeave"))?a.isFunction(e.on_leave_callback)?e.on_leave_callback(t):window.confirm(a.uniform.i18n("on_leave")):void 0})),t.submit(function(){var l,n=!0;return t.removeClass("failedSubmit"),e.ask_on_leave=!1,t.removeClass("askOnLeave"),t.find(e.field_selector).each(function(){a(this).val()===a(this).data("default-value")&&a(this).val("")}),e.prevent_submit||t.hasClass("preventSubmit")?(t.find(e.field_selector).each(function(){a(this).blur()}),a.isFunction(e.submit_callback)&&(n=e.submit_callback(t)),(t.find("."+e.error_class).length||!n)&&(l=a.isFunction(e.prevent_submit_callback)?e.prevent_submit_callback(t,a.uniform.i18n("submit_msg"),[a.uniform.i18n("submit_help")]):a.uniform.showFormError(t,a.uniform.i18n("submit_msg"),[a.uniform.i18n("submit_help")]))):l=!0,a("#qunit-fixture").length&&(l=!1),l===!1&&t.addClass("failedSubmit"),l}),t.find(e.field_selector).on("focus",function(l){var n=a(this);window.console.log(l),t.find("."+e.focused_class).removeClass(e.focused_class),n.closest("."+e.holder_class).addClass(e.focused_class),n.val()===n.data("default-value")&&n.val(""),n.not("select").css("color",n.data("default-color"))}),t.find(e.field_selector).on("blur",function(){var l,i,r=a(this),o=!1,s=n(r,e);if(t.find("."+e.focused_class).removeClass(e.focused_class),(""===r.val()||r.val()===r.data("default-value"))&&!r.hasClass("required"))return r.not("select").css("color",e.default_value_color),r.val(r.data("default-value")),void 0;for(l in a.uniform.validators)if(r.hasClass(l)&&(i=a.uniform.validators[l](r,s,e),o=!0,"string"==typeof i))return r.trigger("uniform-error",i),void 0;o&&r.trigger("uniform-success"),r.css("color",r.data("default-color"))}),t.on("uniform-error",e.field_selector,function(t,n){a.uniform.showValidation(a(this),!1,n,l,e)}),t.on("uniform-success",e.field_selector,function(){a.uniform.showValidation(a(this),!0,"",l,e)}),a("input[autofocus]:first").trigger("focus"),this})},a.uniform.showFormError=function(e,t,l){var n,i;if(a("#errorMsg").length&&a("#errorMsg").remove(),i=a("
").attr("id","errorMsg").html("

"+t+"

"),l.length){i.append(a("
    "));for(n in l)l.hasOwnProperty(n)&&a("ol",i).append(a("
  1. ").text(l[n]))}return e.prepend(i),a("html, body").animate({scrollTop:e.offset().top},500),a("#errorMsg").slideDown(),!1},a.uniform.showFormSuccess=function(e,t){return a("#okMsg").remove(),e.prepend(a("
    ").attr("id","okMsg").html("

    "+t+"

    ")),a("html, body").animate({scrollTop:e.offset().top},500),a("#okMsg").slideDown(),!1},a.uniform.showValidation=function(e,t,l,n,i){var r,o;i=i||a.uniform.defaultOptions,r=e.closest("div."+i.holder_class).andSelf().toggleClass(i.error_class,!t).toggleClass(i.valid_class,t).find("p.form-hint"),o=e.attr("name"),t?o in n&&delete n[o]:n[o]=l,t||r.data("info-text")?t&&(l=r.data("info-text")):r.data("info-text",r.html()),l&&r.html(l)},a.uniform.i18n=function(e){var t,l,n=a.uniform.language[e],i=n.split("%"),r=i[0],o=/^([ds])(.*)$/;for(l=1;i.length>l;l+=1)t=o.exec(i[l]),t&&null!==arguments[l]&&("d"===t[1]?r+=parseInt(arguments[l],10):"s"===t[1]&&(r+=arguments[l]),r+=t[2]);return r},a.uniform.language={required:"%s is required",req_radio:"Please make a selection",req_checkbox:"You must select this checkbox to continue",minlength:"%s should be at least %d characters long",min:"%s should be greater than or equal to %d",maxlength:"%s should not be longer than %d characters",max:"%s should be less than or equal to %d",same_as:"%s is expected to be same as %s",email:"%s is not a valid email address",url:"%s is not a valid URL",number:"%s needs to be a number",integer:"%s needs to be a whole number",alpha:"%s should contain only letters (without special characters or numbers)",alphanum:"%s should contain only numbers and letters (without special characters)",phrase:"%s should contain only alphabetic characters, numbers, spaces, and the following: . , - _ () * # :",phone:"%s should be a phone number",date:"%s should be a date (mm/dd/yyyy)",callback:"Failed to validate %s field. Validator function (%s) is not defined!",on_leave:"Are you sure you want to leave this page without saving this form?",submit_msg:"Sorry, this form needs corrections.",submit_help:"Please see the items marked below.",submit_success:"Thank you, this form has been sent."},a.uniform.defaultOptions={submit_callback:!1,prevent_submit:!1,prevent_submit_callback:!1,ask_on_leave:!1,on_leave_callback:!1,valid_class:"valid",error_class:"error",focused_class:"focused",holder_class:"ctrl-holder",hint_class:"form-hint",field_selector:"input, textarea, select",default_value_color:"#afafaf"}})(jQuery),function(a){a.uniform=a.uniform||{},a.uniform.validators={},a.uniform.validators.required=function(e,t){var l;return e.is(":radio")?(l=e.attr("name"),a("input[name="+l+"]:checked").length?!0:a.uniform.i18n("req_radio",t)):e.is(":checkbox")?(l=e.attr("name"),e.is(":checked")?!0:a.uniform.i18n("req_checkbox",t)):""===a.trim(e.val())?a.uniform.i18n("required",t):!0},a.uniform.validators.validateMinLength=function(e,t){var l=a.uniform.get_val("validateMinLength",e.attr("class"),0);return l>0&&l>e.val().length?a.uniform.i18n("minlength",t,l):!0},a.uniform.validators.validateMin=function(e,t){var l=a.uniform.get_val("validateMin",e.attr("class"),0);return l>parseInt(e.val(),10)?a.uniform.i18n("min",t,l):!0},a.uniform.validators.validateMaxLength=function(e,t){var l=a.uniform.get_val("validateMaxLength",e.attr("class"),0);return l>0&&e.val().length>l?a.uniform.i18n("maxlength",t,l):!0},a.uniform.validators.validateMax=function(e,t){var l=a.uniform.get_val("validateMax",e.attr("class"),0);return parseInt(e.val(),10)>l?a.uniform.i18n("max",t,l):!0},a.uniform.validators.validateSameAs=function(e,t,l){var n,i,r=e.attr("class").split(" "),o="",s="",u=e.closest("form");for(l=l||a.uniform.defaultOptions,i=0;r.length>i;i+=1)if("validateSameAs"===r[i]&&"undefined"!==r[i+1]){o=r[i+1];break}return o&&(n=u.find('input[name="'+o+'"]'),n.val()!==e.val())?(s=a.uniform.get_label_text(n,l),a.uniform.i18n("same_as",t,s)):!0},a.uniform.validators.validateEmail=function(e,t){return e.val().match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)?!0:a.uniform.i18n("email",t)},a.uniform.validators.validateUrl=function(e,t){return e.val().match(/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_\-]*)(\.[A-Z0-9][A-Z0-9_\-]*)+)(:(\d+))?\/?/i)?!0:a.uniform.i18n("url",t)},a.uniform.validators.validateNumber=function(e,t){return e.val().match(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/)||""===e.val()?!0:a.uniform.i18n("number",t)},a.uniform.validators.validateInteger=function(e,t){return e.val().match(/(^-?\d\d*$)/)||""===e.val()?!0:a.uniform.i18n("integer",t)},a.uniform.validators.validateAlpha=function(e,t){return e.val().match(/^[a-zA-Z]+$/)?!0:a.uniform.i18n("alpha",t)},a.uniform.validators.validateAlphaNum=function(e,t){return e.val().match(/\W/)?a.uniform.i18n("alphanum",t):!0},a.uniform.validators.validatePhrase=function(e,t){return""===e.val()||e.val().match(/^[\w\d\.\-_\(\)\*'# :,]+$/i)?!0:a.uniform.i18n("phrase",t)},a.uniform.validators.validatePhone=function(e,t){var l=/^\(?(\d{3})\)?[\- ]?(\d{3})[\- ]?(\d{4})$/;return l.test(e.val())?!0:a.uniform.i18n("phone",t)},a.uniform.validators.validateDate=function(e,t){return e.val().match("([0]?[1-9]|[1][0-2])/([0]?[1-9]|[1|2][0-9]|[3][0|1])/([0-9]{4}|[0-9]{2})$")?!0:a.uniform.i18n("date",t)},a.uniform.validators.validateCallback=function(e,t){var l,n=e.attr("class").split(" "),i="";for(l=0;n.length>l;l+=1)if("validateCallback"===n[l]&&"undefined"!==n[l+1]){i=n[l+1];break}return"undefined"!==window[i]&&"function"==typeof window[i]?window[i](e,t):a.uniform.i18n("callback",t,i)}}(jQuery); \ No newline at end of file +(function(a){a.uniform=function(){},a.uniform.get_val=function(a,e,t){var l,n=t;for(e=e.split(" "),l=0;e.length>l;l+=1)if(e[l]===a&&"undefined"!==e[l+1]&&"val-"===e[l+1].substr(0,4))return n=parseInt(e[l+1].substr(4),10);return n},a.uniform.get_label_text=function(e,t){var l=e.closest("label").text();return t=t||a.uniform.defaultOptions,""===l&&(l=e.closest("div."+t.holder_class).find("label").text()),l.replace("*","").replace(":","")},a.fn.uniform=function(e){return e=a.extend(a.uniform.defaultOptions,e),this.each(function(){var t,l,n,r,i,o;return t=a(this),l=[],n=a.proxy(a.uniform.get_label_text,this),r=a.proxy(a.uniform.get_val,this),i=a.proxy(a.uniform.validate,this),t.find(e.field_selector).each(function(){var t=a(this),l=t.val();t.data("default-color",t.css("color")),l!==t.data("default-value")&&l||(t.not("select").css("color",e.default_value_color),t.val(t.attr("data-default-value")))}),(e.ask_on_leave||t.hasClass("askOnLeave"))&&(o=t.serialize(),a(window).bind("beforeunload",function(){return o!==t.serialize()&&(e.ask_on_leave||t.hasClass("askOnLeave"))?a.isFunction(e.on_leave_callback)?e.on_leave_callback(t):window.confirm(a.uniform.i18n("on_leave")):void 0})),t.submit(function(){var l,n=!0;return t.removeClass("failedSubmit"),e.ask_on_leave=!1,t.removeClass("askOnLeave"),t.find(e.field_selector).each(function(){a(this).val()===a(this).data("default-value")&&a(this).val("")}),e.prevent_submit||t.hasClass("preventSubmit")?(t.find(e.field_selector).each(function(){a(this).blur()}),a.isFunction(e.submit_callback)&&(n=e.submit_callback(t)),(t.find("."+e.error_class).length||!n)&&(l=a.isFunction(e.prevent_submit_callback)?e.prevent_submit_callback(t,a.uniform.i18n("submit_msg"),[a.uniform.i18n("submit_help")]):a.uniform.showFormError(t,a.uniform.i18n("submit_msg"),[a.uniform.i18n("submit_help")]))):l=!0,a("#qunit-fixture").length&&(l=!1),l===!1&&t.addClass("failedSubmit"),l}),t.find(e.field_selector).on("focus",function(){var l=a(this);t.find("."+e.focused_class).removeClass(e.focused_class),l.closest("."+e.holder_class).addClass(e.focused_class),l.val()===l.data("default-value")&&l.val(""),l.not("select").css("color",l.data("default-color"))}),t.find(e.field_selector).on("blur",function(){var l,r,i=a(this),o=!1,s=n(i,e);if(t.find("."+e.focused_class).removeClass(e.focused_class),(""===i.val()||i.val()===i.data("default-value"))&&!i.hasClass("required"))return i.not("select").css("color",e.default_value_color),i.val(i.data("default-value")),void 0;for(l in a.uniform.validators)if(i.hasClass(l)&&(r=a.uniform.validators[l](i,s,e),o=!0,"string"==typeof r))return i.trigger("uniform-error",r),void 0;o&&i.trigger("uniform-success"),i.css("color",i.data("default-color"))}),t.on("uniform-error",e.field_selector,function(t,n){a.uniform.showValidation(a(this),!1,n,l,e)}),t.on("uniform-success",e.field_selector,function(){a.uniform.showValidation(a(this),!0,"",l,e)}),a("input[autofocus]:first").trigger("focus"),this})},a.uniform.showFormError=function(e,t,l){var n,r;if(a("#errorMsg").length&&a("#errorMsg").remove(),r=a("
    ").attr("id","errorMsg").html("

    "+t+"

    "),l.length){r.append(a("
      "));for(n in l)l.hasOwnProperty(n)&&a("ol",r).append(a("
    1. ").text(l[n]))}return e.prepend(r),a("html, body").animate({scrollTop:e.offset().top},500),a("#errorMsg").slideDown(),!1},a.uniform.showFormSuccess=function(e,t){return a("#okMsg").remove(),e.prepend(a("
      ").attr("id","okMsg").html("

      "+t+"

      ")),a("html, body").animate({scrollTop:e.offset().top},500),a("#okMsg").slideDown(),!1},a.uniform.showValidation=function(e,t,l,n,r){var i,o;r=r||a.uniform.defaultOptions,i=e.closest("div."+r.holder_class).andSelf().toggleClass(r.error_class,!t).toggleClass(r.valid_class,t).find("p.form-hint"),o=e.attr("name"),t?o in n&&delete n[o]:n[o]=l,t||i.data("info-text")?t&&(l=i.data("info-text")):i.data("info-text",i.html()),l&&i.html(l)},a.uniform.i18n=function(e){var t,l,n=a.uniform.language[e],r=n.split("%"),i=r[0],o=/^([ds])(.*)$/;for(l=1;r.length>l;l+=1)t=o.exec(r[l]),t&&null!==arguments[l]&&("d"===t[1]?i+=parseInt(arguments[l],10):"s"===t[1]&&(i+=arguments[l]),i+=t[2]);return i},a.uniform.language={required:"%s is required",req_radio:"Please make a selection",req_checkbox:"You must select this checkbox to continue",minlength:"%s should be at least %d characters long",min:"%s should be greater than or equal to %d",maxlength:"%s should not be longer than %d characters",max:"%s should be less than or equal to %d",same_as:"%s is expected to be same as %s",email:"%s is not a valid email address",url:"%s is not a valid URL",number:"%s needs to be a number",integer:"%s needs to be a whole number",alpha:"%s should contain only letters (without special characters or numbers)",alphanum:"%s should contain only numbers and letters (without special characters)",phrase:"%s should contain only alphabetic characters, numbers, spaces, and the following: . , - _ () * # :",phone:"%s should be a phone number",date:"%s should be a date (mm/dd/yyyy)",callback:"Failed to validate %s field. Validator function (%s) is not defined!",on_leave:"Are you sure you want to leave this page without saving this form?",submit_msg:"Sorry, this form needs corrections.",submit_help:"Please see the items marked below.",submit_success:"Thank you, this form has been sent."},a.uniform.defaultOptions={submit_callback:!1,prevent_submit:!1,prevent_submit_callback:!1,ask_on_leave:!1,on_leave_callback:!1,valid_class:"valid",error_class:"error",focused_class:"focused",holder_class:"ctrl-holder",hint_class:"form-hint",field_selector:"input, textarea, select",default_value_color:"#afafaf"}})(jQuery),function(a){var e;a.uniform=a.uniform||{},a.uniform.validators={},e=function(a){return a?a.replace(/([ #;&,.+*~\':"!\^$\[\]()=>|\/@])/g,"\\$1"):a},a.uniform.validators.required=function(t,l){var n;return t.is(":radio")?(n=e(t.attr("name")),a("input[name="+n+"]:checked").length?!0:a.uniform.i18n("req_radio",l)):t.is(":checkbox")?t.is(":checked")?!0:a.uniform.i18n("req_checkbox",l):""===a.trim(t.val())?a.uniform.i18n("required",l):!0},a.uniform.validators.validateMinLength=function(e,t){var l=a.uniform.get_val("validateMinLength",e.attr("class"),0);return l>0&&l>e.val().length?a.uniform.i18n("minlength",t,l):!0},a.uniform.validators.validateMin=function(e,t){var l=a.uniform.get_val("validateMin",e.attr("class"),0);return l>parseInt(e.val(),10)?a.uniform.i18n("min",t,l):!0},a.uniform.validators.validateMaxLength=function(e,t){var l=a.uniform.get_val("validateMaxLength",e.attr("class"),0);return l>0&&e.val().length>l?a.uniform.i18n("maxlength",t,l):!0},a.uniform.validators.validateMax=function(e,t){var l=a.uniform.get_val("validateMax",e.attr("class"),0);return parseInt(e.val(),10)>l?a.uniform.i18n("max",t,l):!0},a.uniform.validators.validateSameAs=function(e,t,l){var n,r,i=e.attr("class").split(" "),o="",s="",u=e.closest("form");for(l=l||a.uniform.defaultOptions,r=0;i.length>r;r+=1)if("validateSameAs"===i[r]&&"undefined"!==i[r+1]){o=i[r+1];break}return o&&(n=u.find('input[name="'+o+'"]'),n.val()!==e.val())?(s=a.uniform.get_label_text(n,l),a.uniform.i18n("same_as",t,s)):!0},a.uniform.validators.validateEmail=function(e,t){return e.val().match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)?!0:a.uniform.i18n("email",t)},a.uniform.validators.validateUrl=function(e,t){return e.val().match(/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_\-]*)(\.[A-Z0-9][A-Z0-9_\-]*)+)(:(\d+))?\/?/i)?!0:a.uniform.i18n("url",t)},a.uniform.validators.validateNumber=function(e,t){return e.val().match(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/)||""===e.val()?!0:a.uniform.i18n("number",t)},a.uniform.validators.validateInteger=function(e,t){return e.val().match(/(^-?\d\d*$)/)||""===e.val()?!0:a.uniform.i18n("integer",t)},a.uniform.validators.validateAlpha=function(e,t){return e.val().match(/^[a-zA-Z]+$/)?!0:a.uniform.i18n("alpha",t)},a.uniform.validators.validateAlphaNum=function(e,t){return e.val().match(/\W/)?a.uniform.i18n("alphanum",t):!0},a.uniform.validators.validatePhrase=function(e,t){return""===e.val()||e.val().match(/^[\w\d\.\-_\(\)\*'# :,]+$/i)?!0:a.uniform.i18n("phrase",t)},a.uniform.validators.validatePhone=function(e,t){var l=/^\(?(\d{3})\)?[\- ]?(\d{3})[\- ]?(\d{4})$/;return l.test(e.val())?!0:a.uniform.i18n("phone",t)},a.uniform.validators.validateDate=function(e,t){return e.val().match("([0]?[1-9]|[1][0-2])/([0]?[1-9]|[1|2][0-9]|[3][0|1])/([0-9]{4}|[0-9]{2})$")?!0:a.uniform.i18n("date",t)},a.uniform.validators.validateCallback=function(e,t){var l,n=e.attr("class").split(" "),r="";for(l=0;n.length>l;l+=1)if("validateCallback"===n[l]&&"undefined"!==n[l+1]){r=n[l+1];break}return"undefined"!==window[r]&&"function"==typeof window[r]?window[r](e,t):a.uniform.i18n("callback",t,r)}}(jQuery); \ No newline at end of file diff --git a/dist/uni-form.jquery.js b/dist/uni-form.jquery.js index 3af9f62..da764ef 100644 --- a/dist/uni-form.jquery.js +++ b/dist/uni-form.jquery.js @@ -1,4 +1,4 @@ -/*! Uni-Form - v1.5.0 - 2013-03-31 +/*! Uni-Form - v1.5.0 - 2013-04-03 * http://sprawsm.com/uni-form/ * Copyright (c) 2013 Dragan Babic; Licensed MIT */ // diff --git a/dist/uni-form.jquery.min.js b/dist/uni-form.jquery.min.js index 765e412..e8b6c8b 100644 --- a/dist/uni-form.jquery.min.js +++ b/dist/uni-form.jquery.min.js @@ -1,4 +1,4 @@ -/*! Uni-Form - v1.5.0 - 2013-03-31 +/*! Uni-Form - v1.5.0 - 2013-04-03 * http://sprawsm.com/uni-form/ * Copyright (c) 2013 Dragan Babic; Licensed MIT */ (function(l){l.fn.uniform=function(a){return a=l.extend({valid_class:"valid",invalid_class:"invalid",error_class:"error",focused_class:"focused",holder_class:"ctrl-holder",field_selector:"input, textarea, select",default_value_color:"#afafaf"},a),this.each(function(){var e,s=l(this);e=function(l,e,s){var t;t=l.closest("div."+a.holder_class).andSelf().toggleClass(a.invalid_class,!e).toggleClass(a.error_class,!e).toggleClass(a.valid_class,e).find("p.form-hint"),e||t.data("info-text")?e&&(s=t.data("info-text")):t.data("info-text",t.html()),s&&t.html(s)},s.submit(function(){s.find(a.field_selector).each(function(){l(this).val()===l(this).data("default-value")&&l(this).val("")})}),s.find(a.field_selector).each(function(){var e=l(this),s=e.val();e.data("default-color",e.css("color")),s!==e.data("default-value")&&s||(e.not("select").css("color",a.default_value_color),e.val(e.data("default-value")))}),s.delegate(a.field_selector,"focus",function(){s.find("."+a.focused_class).removeClass(a.focused_class);var e=l(this);e.parents().filter("."+a.holder_class+":first").addClass(a.focused_class),e.val()===e.data("default-value")&&e.val(""),e.not("select").css("color",e.data("default-color"))}),s.delegate(a.field_selector,"blur",function(){var e=l(this);s.find("."+a.focused_class).removeClass(a.focused_class),""===e.val()||e.val()===e.data("default-value")?(e.not("select").css("color",a.default_value_color),e.val(e.data("default-value"))):e.css("color",e.data("default-color"))}),s.delegate(a.field_selector,"error",function(a,s){e(l(this),!1,s)}),s.delegate(a.field_selector,"success",function(){e(l(this),!0)})})}})(jQuery); \ No newline at end of file diff --git a/src/uni-form-validation.jquery.js b/src/uni-form-validation.jquery.js index c51cf68..48601e0 100644 --- a/src/uni-form-validation.jquery.js +++ b/src/uni-form-validation.jquery.js @@ -172,9 +172,9 @@ // Set the form focus class and remove any classes other than the focus // class and then hide the default label text - $form.find(options.field_selector).on('focus', function (e) { + $form.find(options.field_selector).on('focus', function () { var $input = $(this); -window.console.log(e); + $form // Remove any other focus highlighting .find('.' + options.focused_class) .removeClass(options.focused_class); diff --git a/src/validators.js b/src/validators.js index 8b8c97f..cba29e5 100644 --- a/src/validators.js +++ b/src/validators.js @@ -2,23 +2,28 @@ (function ($) { + var escapeStr; + $.uniform = $.uniform || {}; // Hold a collection of the various form validators $.uniform.validators = {}; + escapeStr = function (str) { + return (str) ? str.replace(/([ #;&,.+*~\':"!\^$\[\]()=>|\/@])/g,'\\$1'): str; + }; + // Value of field is not empty, whitespace will be counted as empty $.uniform.validators.required = function ($field, caption) { var name; if ($field.is(':radio')) { - name = $field.attr('name'); + name = escapeStr($field.attr('name')); if ($("input[name=" + name + "]:checked").length) { return true; } return $.uniform.i18n('req_radio', caption); } if ($field.is(':checkbox')) { - name = $field.attr('name'); if ($field.is(":checked")) { return true; } return $.uniform.i18n('req_checkbox', caption); } diff --git a/test/issues.js b/test/issues.js index ca35718..88f4cc7 100644 --- a/test/issues.js +++ b/test/issues.js @@ -220,7 +220,27 @@ $input.attr('data-default-value'), "A default value of 100.00 should display with decimal point intact." ); + }); + + /** + * Case 17 + * + * Support for input element names with a period + * + * @link https://github.com/LearningStation/uni-form/issues/issue/15 + */ + test("Case 17: Support for input element names with a period", function() { + var $inputs = $('input[name="color"]'), + $form = $('#qunit-form'); + + $inputs.attr({ + name: "demographicInfo.gender" + }); + + $form.uniform(); + $form.submit(); + ok($form.hasClass("failedSubmit"), "Required form failure"); });