1/** 2 * @description Plugin syntax for jQuery. 3 */ 4(function ($) { 5 /** 6 * @description Sets plugin namespace for method calling. 7 * Eg. var isFormValid = $('#myForm').nuIsFormValid(); 8 * @param userOptions Optional options that can be passed in by whatever is calling this. 9 */ 10 $.fn.nuIsFormValid = function(userOptions) { 11 var options = $.extend({}, $.fn.nuIsFormValid.defaults, userOptions); 12 13 // First check is to make sure that the supplied element exists and that there is only one element. 14 if (this == null || this.size() !== 1) { 15 return false; 16 } 17 18 // These checks are to make sure that HTML5 validation is supported in the browser, 19 // and that there are element types to validate. 20 if (!isHtml5ValiditySupported() || !options.validatableElements || options.validatableElements.length === 0) { 21 return true; 22 } 23 24 // Get all of the child elements that are of the supplied validatableElements type. 25 var elementsToValidate = getElementsToValidate(this[0], options.validatableElements); 26 27 // If there are no elements to validate, the form is valid. 28 if (elementsToValidate.length === 0) { 29 return true; 30 } 31 32 // Loop through the child elements and check to see if each is valid. 33 for (var i = 0; i < elementsToValidate.length; i++) { 34 var childElement = elementsToValidate[i]; 35 36 // If a child element is not valid, return false. 37 if (childElement.validity && childElement.validity.valid === false) { 38 return false; 39 } 40 } 41 42 // Return true, meaning all validation passed. 43 return true; 44 }; 45 46 // Plugin defaults. The defaults as a property allow easier manipulation outside of the plugin itself. 47 $.fn.nuIsFormValid.defaults = { 48 validatableElements: ['input', 'textarea'] 49 }; 50 51 function isHtml5ValiditySupported() { 52 var inputElement = document.createElement('input'); 53 return 'validity' in inputElement; 54 } 55 56 function getElementsToValidate(formElement, elementTypes) { 57 var validatableElements = []; 58 59 for (var i = 0; i < elementTypes.length; i++) { 60 var elements = formElement.getElementsByTagName(elementTypes[i]); 61 62 for (var j = 0; j < elements.length; j++) { 63 if (elements[j].validity) { 64 validatableElements.push(elements[j]); 65 } 66 } 67 } 68 69 return validatableElements; 70 } 71}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.