Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jonkemp's full-sized avatar

Jonathan Kemp jonkemp

View GitHub Profile
@jonkemp
jonkemp / reverseIt.js
Last active August 29, 2015 13:56
Reverse a string using JavaScript.
function reverseIt(str) {
return str.split('').reverse().join('');
}
@jonkemp
jonkemp / formatDate.js
Last active August 29, 2015 13:56
Take a date string and return it in a certain format.
function pad(num) {
return (num < 10 ? '0' : '') + num;
}
function formatDateMonthDayYear(date) {
if (date) {
date = new Date(date);
} else {
date = new Date();
}
@jonkemp
jonkemp / babySteps.js
Last active August 29, 2015 13:56
Here's my passing scripts for running learnyounode https://github.com/rvagg/learnyounode#learn-you-the-nodejs-for-much-win.
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 June 8, 2023 17:29
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.
// 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 August 29, 2015 13:56
Tips for using Git.

Tips for using Git

Tagging

Listing your tags

$ git tag

Creating Tags

@jonkemp
jonkemp / formatDecimal.js
Created December 12, 2013 15:33
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.
function getDecimalLimit2(mnt) {
return (Math.round(mnt * 100)) / 100;
}
@jonkemp
jonkemp / isEmpty.js
Created October 11, 2013 18:31
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.
function isEmpty(val) {
return (!/\S/.test(val));
}
@jonkemp
jonkemp / placeholder.js
Last active December 15, 2015 23:09
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…
// Jonathan's placeholder polyfill
// *Requires jQuery
(function () {
function PlaceholderDefaults() {
return {
'placeholderText': '',
'className': ''
};
}
@jonkemp
jonkemp / dateRange.js
Created December 18, 2012 21:58
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.
/**
* 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 June 21, 2012 18:24
scrollDown: Scrolls the page down to show an element if it is not currently in the viewport. Requires jQuery.
function scrollDown( options ) {
var settings, winHeight, elementTop;
// override settings by passing in the options object
settings = $.extend({
'offset': 70,
'selector': 'footer',
'speed': 'fast'
}, options);