|
6 | 6 | *
|
7 | 7 | * Dual licensed under the MIT or GPL Version 2 licenses
|
8 | 8 | *
|
9 |
| -* $version 1.4 |
| 9 | +* $version 1.5 |
10 | 10 | */
|
11 | 11 | (function($) {
|
12 | 12 | $.extend($.fn, {
|
|
180 | 180 | // get updated dialog strings
|
181 | 181 | language = jQueryFormUtils.LANG;
|
182 | 182 |
|
183 |
| - /** |
184 |
| - * Tells whether or not to validate element with this name and of this type |
185 |
| - * |
186 |
| - * @param {String} name |
187 |
| - * @param {String} type |
188 |
| - * @param {Object} config |
189 |
| - * @return {Boolean} |
190 |
| - */ |
191 |
| - var ignoreInput = function(name, type, config) { |
192 |
| - if (type === 'submit' || type === 'button') { |
193 |
| - return true; |
194 |
| - } |
195 |
| - |
196 |
| - for (var i = 0; i < config.ignore.length; i++) { |
197 |
| - if (config.ignore[i] === name) { |
198 |
| - return true; |
199 |
| - } |
200 |
| - } |
201 |
| - return false; |
202 |
| - }; |
203 |
| - |
204 | 183 | /**
|
205 | 184 | * Adds message to error message stack if not already in the message stack
|
206 | 185 | *
|
@@ -778,87 +757,141 @@ jQueryFormUtils.validateDomain = function(val) {
|
778 | 757 | return true;
|
779 | 758 | };
|
780 | 759 |
|
| 760 | +/** |
| 761 | + * <input data-validation="length12" /> => getAttribute($(element).attr('class'), 'length') = 12 |
| 762 | + * @param {String} attrValue |
| 763 | + * @param {String} attrName |
| 764 | + * @returns {Number} |
| 765 | + */ |
| 766 | +jQueryFormUtils.attrInt = function(attrValue, attrName) { |
| 767 | + var regex = new RegExp('(' + attrName + '[0-9\-]+)', "g"); |
| 768 | + return attrValue.match(regex)[0].replace(/[^0-9\-]/g, ''); |
| 769 | +}; |
| 770 | + |
781 | 771 | /**
|
782 | 772 | * Validate the value of given element according to the validation rule
|
783 | 773 | * defined in attribute with given name. Will return true if valid,
|
784 |
| - * error message otherwise |
| 774 | + * error message otherwise. |
785 | 775 | *
|
786 |
| - * @param {jQuery} el |
| 776 | + * Notice! |
| 777 | + * This function is a wrapper around jQueryFormUtils.validate that makes |
| 778 | + * it possible to handle select elements with multiple values |
| 779 | + * |
| 780 | + * @param {jQuery} el - Element containing the value (input,select,textarea) |
787 | 781 | * @param {Object} language (jQueryFormUtils.LANG)
|
788 | 782 | * @param {Object} config
|
789 |
| - * @param {jQuery} form |
| 783 | + * @param {jQuery} form - Optional |
790 | 784 | * @return {String}|{Boolean}
|
791 | 785 | */
|
792 | 786 | jQueryFormUtils.validateInput = function(el, language, config, form) {
|
| 787 | + var values = el.val(); |
| 788 | + values = values || ''; // coerce to empty string if null |
| 789 | + |
| 790 | + if( !(values instanceof Array) ) { |
| 791 | + values = [values]; |
| 792 | + } |
| 793 | + |
| 794 | + // Validate select with multiple values |
| 795 | + if( el.get(0).nodeName == 'SELECT' && el.attr('multiple') ) { |
| 796 | + var validationRules = el.attr(config.validationRuleAttribute); |
| 797 | + var validationErrorMsg = el.attr(config.validationErrorMsgAttribute); |
| 798 | + if(validationRules.indexOf('validate_num_answers') > -1) { |
| 799 | + var num = jQueryFormUtils.attrInt(validationRules, 'num'); |
| 800 | + if(num > values.length) { |
| 801 | + return validationErrorMsg || (language.badNumberOfSelectedOptionsStart +num+ language.badNumberOfSelectedOptionsEnd); |
| 802 | + } |
| 803 | + } |
| 804 | + } |
| 805 | + |
| 806 | + |
| 807 | + for(var i=0; i < values.length; i++) { |
| 808 | + var validation = jQueryFormUtils.validate(values[i], el, language, config, form); |
| 809 | + if(validation !== true) |
| 810 | + return validation; |
| 811 | + } |
| 812 | + |
| 813 | + return true; |
| 814 | +}; |
| 815 | + |
| 816 | +/** |
| 817 | + * Validate the value of given element according to the validation rule |
| 818 | + * defined in attribute with given name. Will return true if valid, |
| 819 | + * error message otherwise |
| 820 | + * |
| 821 | + * @see jQueryFormUtils.validateInput |
| 822 | + * |
| 823 | + * @param {String} value |
| 824 | + * @param {jQuery} el - Element containing the value (input,select,textarea) |
| 825 | + * @param {Object} language (jQueryFormUtils.LANG) |
| 826 | + * @param {Object} config |
| 827 | + * @param {jQuery} form - Optional |
| 828 | + * @return {String}|{Boolean} |
| 829 | + */ |
| 830 | +jQueryFormUtils.validate = function(value, el, language, config, form) { |
793 | 831 |
|
794 |
| - var value = el.val(); |
795 | 832 | var optional = el.attr("data-validation-optional");
|
796 | 833 |
|
797 | 834 | // test if a checkbox forces this element to be validated
|
798 |
| - var validate_if_checked = 0; // set initial value false |
| 835 | + var validationDependsOnCheckedInput = false; |
| 836 | + var validationDependentInputIsChecked = false; |
799 | 837 | // get value of this element's attribute "... if-checked"
|
800 |
| - var validate_if_checked_el_name = el.attr("data-validation-if-checked"); |
801 |
| - // get the form closest to this element |
802 |
| - var thisform = el.closest("form"); |
| 838 | + var validateIfCheckedElementName = el.attr("data-validation-if-checked"); |
803 | 839 | // make sure we can proceed
|
804 |
| - if (validate_if_checked_el_name != null && thisform != null) { |
| 840 | + if (validateIfCheckedElementName != null) { |
| 841 | + |
| 842 | + // Set the boolean telling us that the validation depends |
| 843 | + // on another input being checked |
| 844 | + validationDependsOnCheckedInput = true; |
| 845 | + |
| 846 | + // Form not given as argument |
| 847 | + if(!form) |
| 848 | + form = el.closest("form"); |
| 849 | + |
805 | 850 | // select the checkbox type element in this form
|
806 |
| - var validate_if_checked_el_obj = thisform.find('input[name="' + validate_if_checked_el_name + '"]'); |
807 |
| - // test if it's property "checked" is checked |
808 |
| - if ( validate_if_checked_el_obj.prop('checked') ) |
809 |
| - { // set value for validation checkpoint |
810 |
| - validate_if_checked = 1; |
811 |
| - } |
812 |
| - } // end if depend_checked_el_name not null |
| 851 | + var validateIfCheckedElement = form.find('input[name="' + validateIfCheckedElementName + '"]'); |
813 | 852 |
|
| 853 | + // test if it's property "checked" is checked |
| 854 | + if ( validateIfCheckedElement.prop('checked') ) { |
| 855 | + // set value for validation checkpoint |
| 856 | + validationDependentInputIsChecked = true; |
| 857 | + } |
| 858 | + } |
814 | 859 |
|
815 |
| - // validation checkpoint (added extra criteria depend_check) |
816 |
| - // if empty AND optional AND does not depend on a checkbox being checked, it is ok, return true |
817 |
| - if ((value === null || value.length == 0) && optional === 'true' && !validate_if_checked) { |
| 860 | + // validation checkpoint |
| 861 | + // if empty AND optional attribute is present |
| 862 | + // OR depending on a checkbox being checked AND checkbox is checked, return true |
| 863 | + if ((!value && optional === 'true') || (validationDependsOnCheckedInput && !validationDependentInputIsChecked)) { |
818 | 864 | return true;
|
819 | 865 | }
|
820 | 866 |
|
821 |
| - value = value || ''; // coerce to empty string if null |
822 |
| - |
823 | 867 | var validationRules = el.attr(config.validationRuleAttribute);
|
824 | 868 |
|
825 | 869 | // see if form element has inline err msg attribute
|
826 | 870 | var validationErrorMsg = el.attr(config.validationErrorMsgAttribute);
|
827 | 871 |
|
828 | 872 | if (typeof validationRules != 'undefined' && validationRules !== null) {
|
829 | 873 |
|
830 |
| - /** |
831 |
| - * <input data-validation="length12" /> => getAttribute($(element).attr('class'), 'length') = 12 |
832 |
| - * @param {String} attrValue |
833 |
| - * @param {String} attrName |
834 |
| - * @returns {Number} |
835 |
| - */ |
836 |
| - var getAttributeInteger = function(attrValue, attrName) { |
837 |
| - var regex = new RegExp('(' + attrName + '[0-9\-]+)', "g"); |
838 |
| - return attrValue.match(regex)[0].replace(/[^0-9\-]/g, ''); |
839 |
| - }; |
840 |
| - |
841 | 874 | // Required
|
842 | 875 | if (validationRules.indexOf('required') > -1 && value === '') {
|
843 | 876 | // return custom inline err msg if defined
|
844 | 877 | return validationErrorMsg || language.requiredFields;
|
845 | 878 | }
|
846 | 879 |
|
847 | 880 | // Min length
|
848 |
| - if (validationRules.indexOf('validate_min_length') > -1 && value.length < getAttributeInteger(validationRules, 'length')) { |
849 |
| - return validationErrorMsg || language.tooShortStart + getAttributeInteger(validationRules, 'length') + language.tooShortEnd; |
| 881 | + if (validationRules.indexOf('validate_min_length') > -1 && value.length < jQueryFormUtils.attrInt(validationRules, 'length')) { |
| 882 | + return validationErrorMsg || language.tooShortStart + jQueryFormUtils.attrInt(validationRules, 'length') + language.tooShortEnd; |
850 | 883 | }
|
851 | 884 |
|
852 | 885 | // Max length
|
853 |
| - if (validationRules.indexOf('validate_max_length') > -1 && value.length > getAttributeInteger(validationRules, 'length')) { |
854 |
| - return validationErrorMsg || language.tooLongStart + getAttributeInteger(validationRules, 'length') + language.tooLongEnd; |
| 886 | + if (validationRules.indexOf('validate_max_length') > -1 && value.length > jQueryFormUtils.attrInt(validationRules, 'length')) { |
| 887 | + return validationErrorMsg || language.tooLongStart + jQueryFormUtils.attrInt(validationRules, 'length') + language.tooLongEnd; |
855 | 888 | }
|
856 | 889 |
|
857 | 890 | // Length range
|
858 | 891 | if (validationRules.indexOf('validate_length') > -1) {
|
859 |
| - var range = getAttributeInteger(validationRules, 'length').split('-'); |
| 892 | + var range = jQueryFormUtils.attrInt(validationRules, 'length').split('-'); |
860 | 893 | if (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10)) {
|
861 |
| - return validationErrorMsg || language.badLength + getAttributeInteger(validationRules, 'length') + language.tooLongEnd; |
| 894 | + return validationErrorMsg || language.badLength + jQueryFormUtils.attrInt(validationRules, 'length') + language.tooLongEnd; |
862 | 895 | }
|
863 | 896 | }
|
864 | 897 |
|
@@ -976,7 +1009,9 @@ jQueryFormUtils.LANG = {
|
976 | 1009 | badCustomVal : 'You gave an incorrect answer',
|
977 | 1010 | badInt : 'Incorrect integer value',
|
978 | 1011 | badSecurityNumber : 'Your social security number was incorrect',
|
979 |
| - badUKVatAnswer : 'Incorrect UK VAT Number' |
| 1012 | + badUKVatAnswer : 'Incorrect UK VAT Number', |
| 1013 | + badNumberOfSelectedOptionsStart : 'You have to choose at least ', |
| 1014 | + badNumberOfSelectedOptionsEnd : ' answers' |
980 | 1015 | };
|
981 | 1016 |
|
982 | 1017 |
|
|
0 commit comments