Posts

Showing posts with the label JavaScript

Validate Date

Snippet: var msie = window.document.documentMode; /**** * Checks a controls value to see if it is a Date Value or not * * Parameters: * @cntrl - String; the Control ID value * @success - Function(); The Success function to execute * @fail - Function(); The Fail function to execute */ function ValidDate(cntrl, success, fail) { var valid = false; if (msie !== undefined) { var dtString = cntrlIDs[cntrl].value; var date = new Date(dtString); if (!isNaN(date)) { dtString = dtString.replace(/-/g, "/"); // Assumed pattern: MM/dd/CCYY var parts = dtString.split("/") var dd = parseInt(parts[1], 10); // Day Number var mm = parseInt(parts[0], 10); // Month Number var yr = parseInt(parts[2], 10); // Year Number if (dd === date.getDate() ...

Acquiring List of controls - Classic JS

Recently ran into a scenario where I need to use the low-level JavaScript parser, instead of JQuery.  During this venture, I ran across a situation where it would be easier for me to select all the HTML Elements with an ID, assign it to a variable as JSON, and then operate on it from that point. So using my previous short-hand experience with JQuery, I started constructing a similar type of format to be used. *********** JQuery *************** // One line and your done, pretty much var cntrls = $('[id]') // Later on, you simply access the element from the variable var val = cntrls['someID'].value *********** JavaScript *********** // Not so one-liner; put in the DOM.Ready event for JS var cntrlIDs = {} var root = document.getElementById('rootID').querySelectorAll('*'); for (i = 0, n = root.length; i < n; ++i) { var el = root[i]; if (el.id) { cntrlIDs[el.id] = el; } } // Later...

Execute Functions...solo or Array

I have found myself multiple times having to "re-invent" this methodology to execute a single pass-through function or an array, pending on the situation.   For myself, and to share with others, I am posting this code-snippet for consumption. /**** @func - function() or [function(), function(),...] ****/ function ExecuteFunction( func ) { if ( typeof func !== 'undefined' && func != null ) { if ( typeof func === 'function' ) { func(); } else { if ( func instanceof Array ) { func.forEach( function ( fn ) { ExecuteFunction( fn ); } ); } } } } And yes you can have a nested array of functions.  If some crazy reason you wanted to do this: var func = [ function(){ //do something }, [ function () { ...

JSON/AJAX Helpers

Recently, had an implementation where I needed to implement ASP.Net Web API 2 and accompanied it with Javascript/JQuery AJAX communication.  In doing this i found myself making the same pattern calls with some varying value for certain properties for the AJAX data calls.  As such, i ended up building a few helper functions to simply the inclusion of these various minor quirks based on what was needed.  Below  you will find the various forms. 1: /********************************************************************* 2: @type - dataType, such as 'json', 'xml', 'script', 'jsonp' or 'html' 3: @contentType - string value, represents the HTML type of content 4: IE: 'application/json; charset=utf-8' 5: @url - API URL, such as 'api/tasks' should be constrainted to the 6: Web API Controller 7: @success - Function(data, status, xhr) handler, The logic to execute 8: on a 200 res...

Mustache Helper functions

Recently, implemented the Mustache Template processing framework.  As i worked through this implementation, i found myself creating the same JavaScript call and decided to simplify the overall execution into a single function signature and just call it multiple times. Root Mustache Call: 1: /********************************************************************* 2: @file - string value, Should be the HTML Template file. 3: @filterid - string value, Should the ID value of the Script tag inside 4: @file to obtain the HTML content. 5: @api - string value, Should be the URL to the API request 6: @dest - string value, Should be the destination selector, ideally ID 7: value of the control to load the Mustache Template into. 8: @func - (Nullable) Function value, If defined this will be executed 9: after the loading of the data into the Template. 10: --------------------------------------------------------------------...