DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Full-Stack Observability Essentials: Explore the fundamentals of system-wide observability and key components of the OpenTelemetry standard.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Exploring Shadow DOM With Examples Using Cypress
  • Demystifying the ‘Class’ Attribute in HTML: A Comprehensive Guide With Examples
  • Cracking the Code: 7 Secrets Every Web Developer Should Know
  • Doubly Linked List in Data Structures and Algorithms

Trending

  • Effective Tips for Debugging Complex Code in Java
  • How DevOps Can Cost You Millions if Not Implemented the Right Way
  • .NET Performance Optimization Techniques for Expert Developers
  • Helm Dry Run: Guide and Best Practices
  1. DZone
  2. Coding
  3. Languages
  4. Proof of Concept: Live HTML checking for a textarea

Proof of Concept: Live HTML checking for a textarea

Raymond Camden user avatar by
Raymond Camden
·
Sep. 15, 14 · Interview
Like (0)
Save
Tweet
Share
3.32K Views

Join the DZone community and get the full member experience.

Join For Free

most online forms don't allow html, or allow a very strict subset of html, but for years now my blog form (the one i'm using right now) has allowed for any and all html. i figure if i can't trust myself, who can i trust? of course, from time to time i screw up and forget to close a tag or make some other mistake. for a while now i've wondered - is there an easy way to check for that and potentially catch those mistakes before i save the form?

i currently know of two services that let you check html. one is the w3c validator . i've used this for one of my brackets extensions ( w3cvalidation ) and it works ok, but on simple tests it seemed to miss obvious things. for example, given input of: <b>moo it only notices the lack of a root docttype and declaration and basically throws up. since my input will always be part of a full html page, the blog content, this didn't seem appropriate. also, it would be asynchronous and i wanted something i could run entirely client-side.

i then decided to check out htmlhint . htmlhint provides rules-based reports on html input. it is pretty simple to use and - surprise surprise - i also have a brackets extension for it ( brackets-htmlhint ). since i could use it entirely client-side, i thought i'd check it out. i built a simple demo that did validation on keyup.

first, i created a simple html page and form.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="htmlhint.js"></script>
<script src="app.js"></script>
<style>
#blogcontent {
	width: 500px;
	height: 300px;
}
#htmlissues {
	font-weight: bold;
}
</style>
</head>
<body>

	<form>
	<textarea id="blogcontent"></textarea>
	<div id="htmlissues"></div>
	</form>
	
</body>
</html>

nothing really special here. you can see the textarea as well as an empty div i plan on using for validation. now let's look at the code.

$(document).ready(function() {
	$issuediv = $("#htmlissues");
	
	//from docs:
	var rules = {
		"tagname-lowercase": true,
		"attr-lowercase": true,
		"attr-value-double-quotes": true,
		"doctype-first": false,
		"tag-pair": true,
		"spec-char-escape": true,
		"id-unique": true,
		"src-not-empty": true,
		"attr-no-duplication": true
	}
	
	function htmlescape(s) {
		s = s.replace(/\</g, "<");
		s = s.replace(/\>/g, ">");
		return s;
	}
	
	$("#blogcontent").on("keyup", function() {
		var content = $(this).val();
		var issues = htmlhint.verify(content, rules);
		
		if(issues.length === 0) {
			$issuediv.html("");
			return;
		}
		console.dir(issues);
		var s = "possible html issues found:<ul>";
		for(var i=0, len=issues.length; i<len; i++) {
			s += "<li>"+htmlescape(issues[i].message)+"</li>";
		}
		s += "</ul>";
		$issuediv.html(s);
		
	});
});

from the top, i'm caching my results div so i can reuse it doing validation. i modified htmlhint's default rule set to turn off the doctype requirement. (because, again, i'm validating a small part of a page, not the entire page.) and that's basically it. i run the library's verify method and just render out the results. here is an example of it in action:

as you can see, it found the unmatched tag pair, but didn't notice that turd was an invalid tag. to be honest, i'm much more concerned about screwing up tag pairs so that makes me happy enough.

if you want to play with it yourself, i set up a demo below.

HTML Concept (generic programming)

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Shadow DOM With Examples Using Cypress
  • Demystifying the ‘Class’ Attribute in HTML: A Comprehensive Guide With Examples
  • Cracking the Code: 7 Secrets Every Web Developer Should Know
  • Doubly Linked List in Data Structures and Algorithms

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: