Skip to content

Instantly share code, notes, and snippets.

@mathiasbynens
Forked from 140bytes/LICENSE.txt
Created June 11, 2011 08:56
Show Gist options
  • Save mathiasbynens/1020383 to your computer and use it in GitHub Desktop.
Save mathiasbynens/1020383 to your computer and use it in GitHub Desktop.
Quick and dirty way to check if a given string can be used as an unquoted attribute value in HTML. (50 bytes)
So, after cross-referencing these three different sections of the HTML
spec, we can finally conclude that a valid unquoted attribute value in
HTML is any string of text that is not the empty string and that doesn’t
contain spaces, tabs, line feeds, form feeds, carriage returns, ", ', `,
=, <, or >.
→ See http://mathiasbynens.be/notes/unquoted-attribute-values
function(value) {
return /^[^ \t\n\f\r"'`=<>]+$/.test(value);
}
function(v){return/^[^ \t\n\f\r"'`=<>]+$/.test(v)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mathias Bynens <http://mathiasbynens.be/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "unquotedHTMLAttributeValueValidator",
"description": "Check if a given string can be used as an unquoted attribute value in HTML.",
"keywords": [
"html",
"attribute",
"attribute-values"
]
}
<!DOCTYPE html>
<!-- Demo: http://mothereffingunquotedattributes.com/ -->
<title>Unquoted attribute value validator</title>
<input value="foo'bar" required autofocus> is <em>not</em> a valid unquoted attribute value.
<script>
var isUnquotable = function(v){return/^[^ \t\n\f\r"'`=<>]+$/.test(v)},
el = document.getElementsByTagName('em')[0];
document.getElementsByTagName('input')[0].oninput = function() {
el.style.display = isUnquotable(this.value) ? 'none' : 'inline';
};
</script>
@maettig
Copy link

maettig commented Nov 12, 2011

It's true. If you want to stick with the specification where certain characters do not count as whitespace characters, you can not use \s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment