Skip to content

Instantly share code, notes, and snippets.

@salsalabs
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salsalabs/d98080ad6ee9395b4775 to your computer and use it in GitHub Desktop.
Save salsalabs/d98080ad6ee9395b4775 to your computer and use it in GitHub Desktop.
Parse normal-looking dates (like 4/5/67) and format them so that Cosm will accept them. The script finds all of the Salsa date fields and adds an onBlur handler to parse and format. Invalid dates are left empty. Doing this relieves the supporter of trying to remember to put in YYYY-MM-DD. This script uses moment.js (http://momentjs.com) by loadi…
<script>
// Script to parse Salsa date fields. This handles the case
// where customers want to use a natural format (4/5/67). Without
// this, error messages can hang stuff up.
// @dependencies http://momentjs.com/ (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js)
//
$(document).ready(function() {
var cdnUrl = 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js';
function setup() {
$('.salsa_date_input').blur(function() {
var text = $(this).val();
if (text.length == 0) { return; }
var date = moment(text).format('YYYY-MM-DD')
date = date == 'Invalid date' ? '' : date;
$(this).val(date);
})
}
if (typeof(moment) == 'undefined') {
$.getScript( cdnUrl, function( data, textStatus, jqxhr ) { setup() });
} else {
setup();
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment