Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Kemp jonkemp

View GitHub Profile
@jonkemp
jonkemp / reverseIt.js
Last active Aug 29, 2015
Reverse a string using JavaScript.
View reverseIt.js
function reverseIt(str) {
return str.split('').reverse().join('');
}
@jonkemp
jonkemp / formatDate.js
Last active Aug 29, 2015
Take a date string and return it in a certain format.
View formatDate.js
function pad(num) {
return (num < 10 ? '0' : '') + num;
}
function formatDateMonthDayYear(date) {
if (date) {
date = new Date(date);
} else {
date = new Date();
}
View babySteps.js
var args = process.argv.slice(2);
var total = 0;
for (var i = 0, len = args.length; i < len; i++) {
total += Number(args[i]);
}
console.log(total);
@jonkemp
jonkemp / validate-currency.js
Last active May 29, 2019
Currency validation method for the jQuery Validation plugin. Decimal place is optional but if included, it requires 2 places. Also, the dollar sign is optional.
View validate-currency.js
// Validation method for US currency
$.validator.addMethod("currency", function (value, element) {
return this.optional(element) || /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/.test(value);
}, "Please specify a valid amount");
@jonkemp
jonkemp / git-best-practices.md
Last active Aug 29, 2015
Tips for using Git.
View git-best-practices.md

Tips for using Git

Tagging

Listing your tags

$ git tag

Creating Tags

@jonkemp
jonkemp / formatDecimal.js
Created Dec 12, 2013
Format decimal numbers to prevent rounding errors. The first function limits decimal numbers to 2 places. The function always returns the number in number format even if you input the number as a string.
View formatDecimal.js
function getDecimalLimit2(mnt) {
return (Math.round(mnt * 100)) / 100;
}
@jonkemp
jonkemp / isEmpty.js
Created Oct 11, 2013
Checks a string to see if it is empty. Returns false if it finds anything other than white space in a string. Otherwise it returns true.
View isEmpty.js
function isEmpty(val) {
return (!/\S/.test(val));
}
@jonkemp
jonkemp / placeholder.js
Last active Dec 15, 2015
Placeholder polyfill for inputs. Detects support for the placeholder attribute on inputs. If it is not present, the placeholder attribute text is inserted into the input, and a class of "placeholder-text" is added to the input. On keydown on the input, the placeholder text and the class is removed. On blur, if the input is empty, the placeholder…
View placeholder.js
// Jonathan's placeholder polyfill
// *Requires jQuery
(function () {
function PlaceholderDefaults() {
return {
'placeholderText': '',
'className': ''
};
}
@jonkemp
jonkemp / dateRange.js
Created Dec 18, 2012
Get the current date and the date 30 days ago. Then format the dates like this: M/D/YYYY. This an example of date manipulation in JavaScript.
View dateRange.js
/**
* Get the current date and the date 30 days ago.
* Then format the dates like this: M/D/YYYY.
*/
var diff = 2592000000;
var now = new Date();
var diffDate = new Date( now.getTime() - diff );
var nowPrint = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ].join("/");
var diffPrint = [ diffDate.getMonth() + 1, diffDate.getDate(), diffDate.getFullYear() ].join("/");
@jonkemp
jonkemp / scrolldown.js
Created Jun 21, 2012
scrollDown: Scrolls the page down to show an element if it is not currently in the viewport. Requires jQuery.
View scrolldown.js
function scrollDown( options ) {
var settings, winHeight, elementTop;
// override settings by passing in the options object
settings = $.extend({
'offset': 70,
'selector': 'footer',
'speed': 'fast'
}, options);