I have the following javascript code

var list = [
  Date : '2014-12-31 12:23:43',
  DateStart: '1980-12-30 23:43:42',
  Name: 'Kate',
  Date : '1978-05-21 23:43:65',
  DateStart: '1973-04-06 12:34:09',
  Name: 'John',

And the following code to verify for regex pattern:

for (var i in list) {
var data = [];
if (/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.test(list[i].?)) {
      data.push({ Data: list[i].Data });

What can I do to all columns be verified for the date pattern?

Making the array date stay only with the values 2014-12-31 12:23:43,1980-12-30 23:43:42,1978-05-21 23:43:65,1973-04-06 12:34:09

Recommended Answers

All 2 Replies

If your current list is what you said, to access it would be...

var len = list.length;
for (var i=len-1; i>=0; i--) {  // for removing data
  var data = list[i];
  // check Date existence
  if (!data["Date"]) {
    // do something, for example, alert and remove it
    alert("Invalid date data at index "+i);
    list.splice(i, 1);  // remove the item at index i
  // check Date pattern
  if (!data["Date"].match(/^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})/)) {
    // do something because date format is invalid
    // you could remove the element as above
  // up until now, the date format is correct
  // BUT it does not guarantee that the value is valid
  // i.e. 1234-99-99 will pass through the above checks
  // you may need to do something

Watch out if you are dynamically removing element from an array you are iterating through. The above method would have no problem as long as 1)you iterate from back to front and 2)you do not change the value of iteration variable (i) wihing the loop.

if (/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.test(list[i].?))

What's the dot + questionmark doing there?
Remove that, and come again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.