JavaScript: Age Validation Form

Published on

This form validates the date selected by the user is valid and that the user is at least the age of 18. (Note that it does not dynamically adjust the day select list based on the month and year selected.) HTML <form id=”age-validation-form” class=”form-inline” method=”POST”> <div class=”form-group”> <select name=”dob_month” id=”dob_month” class=”required form-control”> <option value=””>Month</option> <option … Continue reading JavaScript: Age Validation Form

JavaScript: Age Validator

Published on

function isOfAge(year, month, day, age) { age = age || 18; var mydate = new Date(); mydate.setFullYear(year, month-1, day); var currdate = new Date(); currdate.setFullYear(currdate.getFullYear() – age); if ((currdate – mydate) < 0) { return false; } return true; } References: Validate Age | CSS-Tricks

JavaScript: Image onerror Fallback

Published on

Without a JavaScript function <img src=”image.png” onError=”this.onerror=null;this.src=’/images/noimage.gif’;” /> or With a JavaScript function function imgError(image) { image.onerror = “”; image.src = “/images/noimage.gif”; return true; } <img src=”image.png” onerror=”imgError(this);”/> For a compatibility lists of browsers that support the error facility: http://www.quirksmode.org/dom/events/error.html

JavaScript Foundations

Published on

JavaScript Written in 1995 by Bendan Eich at Netscape. Intended as a lightweight alternative to Java applets for smaller bits of interaction and functionality No relationship between JavaScript and Java Interpreted, not compiled Compiled languages are written in source code that is run through a compiler to create machine-readable object code which is often run … Continue reading JavaScript Foundations

JavaScript Design Patterns

Published on

Constructor Pattern Object constructors are used to create specific types of objects. Examples: var newObject = {}; // or var newObject = Object.create( Object.prototype ); // or var newObject = new Object(); Module Pattern Provides both private and public encapsulation. Used to emulate the concept of classes. Options for implementing modules include: The Module pattern … Continue reading JavaScript Design Patterns

JavaScript Essential Functions

Published on

debounce getAbsoluteUrl getUrlParameter insertRule isNative matchSelector once poll randomRange stripTags 1. debounce // Returns a function, that, as long as it continues to be invoked, will not // be triggered. The function will be called after it stops being called for // N milliseconds. If `immediate` is passed, trigger the function on the // leading … Continue reading JavaScript Essential Functions

JavaScript: stripTags

Published on

/* Strip html tags. reference: http://davidwalsh.name/essential-javascript-functions */ function stripTags(htmlstr) { var div = document.createElement(‘div’); div.innerHTML = htmlstr; return div.textContent; }

JavaScript: randomRange

Published on

/* Returns a random integer or float in a specified range. Examples: console.log(rand(2,5)); // float random between 2 and 5 inclusive console.log(rand(1,100,true)); // integer random between 1 and 100 reference: http://davidwalsh.name/essential-javascript-functions */ function rand(min, max, integer) { var r = Math.random() * (max – min) + min; return integer ? r|0 : r; }