Validate HTML on local machine

Viewed 50290

I'm currently trying to learn HTML and Java EE Servlet programming. I have an application server running on my local machine (Orion Application Server) and I'm connecting to web pages I've deployed on this server using a browser running on the same machine, directed to http://localhost/mypage.htm (for example).

I know W3C has a site you can go to that will validate an HTML page (and count how many errors are found for a given doctype), but that has to be a publicly available URL. How do you validate HTML on a locally running setup like I've described above?

10 Answers

If you're using node you may use package html-validator

const validator = require('html-validator')
const fs = require('fs')
var options = {
  format: 'text'
}

fs.readFile( 'file-to-validate.html', 'utf8', (err, html) => {
  if (err) {
    throw err;
  }

  options.data = html

  validator(options)
    .then((data) => {
      console.log(data)
    })
    .catch((error) => {
      console.error(error)
    })
})

You can run the tool on your local with docker just by using the command below.

  • docker run -it --rm -p 8888:8888 ghcr.io/validator/validator:latest

After running it with docker, when you go to 127.0.0.1:8888 you will see the validator tool. When you try to validate a url and if you get such an error like IO Error (Connection refused) then you can try installing vnu with brew by using the second command below.

  • brew install vnu

I tried it with docker and I got IO Error. Then I tried it with brew and it was successful. After you install it with brew, now to check a url you should run the command below.

  • vnu http://localhost/page-to-test/

Just replace the url with the one you want to validate with the tool.

Related