How could I manipulate this array in Javascript for this kind of error checking?

前端 未结 4 1806
时光取名叫无心
时光取名叫无心 2021-01-28 05:47

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 ar

4条回答
  •  礼貌的吻别
    2021-01-28 06:50

    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"]
    

提交回复
热议问题