Skip to content

Instantly share code, notes, and snippets.

@dezinezync
Created February 4, 2016 09:48
Show Gist options
  • Select an option

  • Save dezinezync/ad15e435aa2c9967ab2d to your computer and use it in GitHub Desktop.

Select an option

Save dezinezync/ad15e435aa2c9967ab2d to your computer and use it in GitHub Desktop.
Check for Input type support
// This is an adapted version from http://www.quirksmode.org/blog/archives/2015/03/better_modern_i.html
/**
* Supports Type determines whether the give input type is supported in the browser
* @param {String} type The type to check for. Example: text, date, time, color
* @return {Boolean} Returns true if the input type is supported, false otherwise.
*/
function supportsType(type) {
var input = document.createElement("input")
input.setAttribute("type", type)
var desiredType = input.getAttribute('type')
var supported = false
if (input.type === desiredType)
supported = true
input.value = 'Hello world'
var helloWorldAccepted = (input.value === 'Hello world')
switch (desiredType) {
case "email":
case "url":
if (!input.validationMessage)
supported = false
break
case "color":
case "date":
case "datetime":
case "datetime-local":
case "month":
case "number":
case "time":
case "week":
if (helloWorldAccepted)
supported = false
break
}
return supported
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment