0

How can I make Javascript check an array and make sure that there's no error committed after the comma separating each element of the array .. For example a user may form an array with two commas in a row separating an element instead of one comma .. Not just comma , any other wrong input.

var cars=["Saab",,"Volvo","BMW"]; // Here , this array has an error , there's a double comma .
var cars=["Saab",*"Volvo","BMW"]; // this array has an error , there's an asterix.
var cars=["Saab","Volvo","BMW"];  // this array is the right one.

Here's the wrong array

What Javascript function or solution could I use to make sure an array is checked and no error is within it .. Only elements and separating commas with no space in between.

Your help would be so much appreciated.

2
  • You want to validate that a string is a valid javascript code ? Commented Mar 25, 2013 at 17:38
  • That already throws errors Commented Mar 25, 2013 at 17:39

4 Answers 4

3

Given that you are trying to validate user-entered data, I'd suggest reevaluating your approach.

You should not require end-users to enter data in valid JavaScript syntax; this is annoying to your users.

Instead, I'd just have a textarea and instructions.

mockup

This is far easier for humans to deal with, and parsing is super simple:

var cars = document.getElementById('carstextarea').value.split('\n');
// => ["Saab","Volvo","BMW"]
Sign up to request clarification or add additional context in comments.

Comments

1

Use a static code analyzer such as JSLint or JSHint.

The latter is available as a JavaScript library in addition to the web-hosted flavor.

7 Comments

ok .. but could you help as far as implementing the solution for this error checking ?
I am trying to control error checking for my application online .. not to check my own code
What do you mean by "control error checking for my application online?" Where does this code come from? It is user-entered?
Yes .. it is used-entered .. so I need to implement a function for it
Then just use the browser bundle. Be very careful with accepting & executing arbitrary JavaScript code, however, as this is a massive security hole.
|
0

You can use jslint takes a JavaScript source and scans it ,

visual studio, netbeans or eclipse have a plugin for that.

Good Luck!!!

1 Comment

I am trying to control error checking for my application online .. not to check my own code
0

Your second cars example with the * in it won't compile.

To check for double commas, you can iterate over the array and check for an undefined value.

for(var i = 0; i<cars.length; i++) {
if(cars[i] === undefined) {
// do something
}
}

Alternatively you could try and catch this before you generate the array. Is there a way to check for an empty row inputted by the user?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.