Domanda

I am trying to build a simple JavaScript validator, something like JSHint. I want to write a JavaScript code and click a "Validate" button, and the errors should show up.

Passing the JavaScript source like this JSHINT(js-source) just gives you "true" or "false" depending on whether the code is valid or not. I would like to get the details (e.g. line number where the error ocurred, error type etc.) of each error.

Here's what I already have.

http://jsbin.com/AZEpOHi/1/edit

È stato utile?

Soluzione

From the docs:

If your input passes JSHint tests, the function will return true. Otherwise, it will return false. In that case, you can use JSHINT.errors to retrieve the errors or request a complete report by calling the JSHINT.data() method.

The errors are in the JSHINT.errors array, and those objects include line numbers and error reasons.

Here's a jsfiddle that demonstrates a simple use of JSHINT.errors.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top