Skip to content

Instantly share code, notes, and snippets.

@Langmans
Last active August 29, 2015 14:14
Show Gist options
  • Save Langmans/a32532777c167e275896 to your computer and use it in GitHub Desktop.
Save Langmans/a32532777c167e275896 to your computer and use it in GitHub Desktop.
input type checking (datetime, email, etc)
jQuery(function ($) {
if (!inputsupport('datetime-local')) {
$('[type=datetime-local]').each(function () {
var $input = $(this), placeholder = $input.attr('placeholder');
$input.attr({
type: 'text',
placeholder: this.hasAttribute('placeholder') ? placeholder + ' (JJJJ-MM-DD UU:MM)' : 'JJJJ-MM-DD UU:MM',
pattern: '20\\d{2}(-|\/)((0[1-9])|(1[0-2]))(-|\/)((0[1-9])|([1-2][0-9])|(3[0-1]))(T|\\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9])(:([0-5][0-9]))?'
});
});
}
});
var inputsupport = (function () {
// private variables.
var support = {}, input;
// the actual check.
return function (type) {
if (!support.hasOwnProperty(type)) {
input = input || document.createElement('input');
input.type = type;
// input.type is set back to text if not supported,
// so we can just compare the type parameter with the input type property/attribute.
support[type] = input.type == type;
}
return support[type];
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment