Solved

JavaScript: Check if all open HTML tags are closed

Posted on 2011-02-21
1
1,242 Views
Last Modified: 2012-05-11
The code below finds all opening and closing HTML tags.  I want to check if all tags which are opened are correctly closed.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript">
/*<![CDATA[*/

function checkTags(v) {
 return(v.match(/(<+[^>]*?>)/g));
}

alert( checkTags('<div><h1>Hello <strong>World</strong></h1></div>')); // Should return true because all open tags are closed

alert( checkTags('<div><h1>Hello <strong>World</trsong></h1></div>')); // Should return false because not all open tags are correctly closed

/*]]>*/
</script>

</head>
<body>

<h1>I want to check and make sure that all open tags are closed.</h1>
<h2>The function should return true if all open tags are closed and false if not all open tags are closed.</h2>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 12

Accepted Solution

by:
Amick earned 500 total points
ID: 34944072
Increment when you see opening tags
decrement when you see matching closing tags
if opening tags accumulator - closing tag accumulator should = 0 return true, else false.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular - following an example by an expert here (Julian) 11 29
JS Plugin 4 29
My dialog box isnt work, no box displays but text does 8 38
Javascript create file issue 4 48
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now