Categories
JavaScript Answers

How to validate dates in format MM-DD-YYYY with JavaScript?

Sometimes, we want to validate dates in format MM-DD-YYYY with JavaScript.

In this article, we’ll look at how to validate dates in format MM-DD-YYYY with JavaScript.

How to validate dates in format MM-DD-YYYY with JavaScript?

To validate dates in format MM-DD-YYYY with JavaScript, we can use a regex to get the date parts.

And then we can compare the parts when we put them into the Date constructor to create a new date from them.

For instance, we write:

const isValidDate = (date) => {
  const matches = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/.exec(date);
  if (matches === null) {
    return false;
  }
  const [_, m, d, y] = matches
  const composedDate = new Date(+y, +m - 1, +d);
  return composedDate.getDate() === +d &&
    composedDate.getMonth() === +m - 1 &&
    composedDate.getFullYear() === +y;
}
console.log(isValidDate('10-12-1961'));

We use the /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/ regex to extract the date parts with the exec method.

Then we destructure the matches and assign the month to m, day to d, and year to y.

Next, we create a new date object with the Date constructor with the date parts.

We’ve to subtract 1 from m to conform with with the Date constructor accepts as the month value.

Finally, we compare the date, month and year from the composedDate object with the values extracted from the regex.

We use the unary + operator to convert each value to numbers.

Therefore, the console log should log true.

Conclusion

To validate dates in format MM-DD-YYYY with JavaScript, we can use a regex to get the date parts.

And then we can compare the parts when we put them into the Date constructor to create a new date from them.

Categories
JavaScript Answers

How to add a dialog box with jQuery and make it show more than once?

Sometimes, we want to add a dialog box with jQuery and make it show more than once.

In this article, we’ll look at how to add a dialog box with jQuery and make it show more than once.

How to add a dialog box with jQuery and make it show more than once?

To add a dialog box with jQuery and make it show more than once, we can use the jQuery UI’s dialog method with the modal option set to true.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />

<button>
  show
</button>

<div id="dialog" title="Basic dialog">
  <p>hello world.</p>
</div>

We add the show button and a div for the dialog.

Then we write:

$(() => {
  $("#dialog").hide()

  $('button').click(() => {
    $("#dialog")
      .dialog({
        modal: true,
      })
  })
});

We select the dialog div with $("#dialog").

And we call hide on it to hide it initially.

Then we select the button with $('button').

And we call click on it with a callback that calls dialog with an object with modal set to true.

Now when we click the button, we see the dialog displayed. And we can close it with the ‘x’ button and click show to open it again.

Conclusion

To add a dialog box with jQuery and make it show more than once, we can use the jQuery UI’s dialog method with the modal option set to true.

Categories
JavaScript Answers Lodash

How to get duplicate values from an array with Lodash?

Sometimes, we want to get duplicate values from an array with Lodash.

In this article, we’ll look at how to get duplicate values from an array with Lodash.

How to get duplicate values from an array with Lodash?

To get duplicate values from an array with Lodash, we can use the filter and includes methods.

For instance, we write:

const arr = [1, 2, 2, 3, 3]
const dups = _.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1));

console.log(dups)

We have an arr array that has some duplicate values.

Then we call filter with arr and a callback that calls includes to check if the array has the duplicate entries of val.

Therefore, dups is [2, 3].

Conclusion

To get duplicate values from an array with Lodash, we can use the filter and includes methods.

Categories
JavaScript Answers

How to check which element has been clicked with jQuery?

Sometimes, we want to check which element has been clicked with jQuery.

In this article, we’ll look at how to check which element has been clicked with jQuery.

How to check which element has been clicked with jQuery?

To check which element has been clicked with jQuery, we can add a click event listener to the body element.

Then we can check which element is clicked inside the click event handler with the is method.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<p>
  foo
</p>

<div>
  bar
</div>

<section>
  baz
</section>

to add a few elements into the page.

Then we write:

$('body').click((e) => {
  const target = $(e.target);
  if (target.is('p')) {
    console.log('p clicked')
  } else if (target.is('div')) {
    console.log('div clicked')
  } else if (target.is('section')) {
    console.log('section clicked')
  }
});

We select the body element with $.

Then we call click on it with a callback that gets the clicked element with the e.target property.

Then we check for what’s clicked with the is method called with the selector we’re checking for.

Therefore, when we click on different elements, we should see different text logged.

Conclusion

To check which element has been clicked with jQuery, we can add a click event listener to the body element.

Then we can check which element is clicked inside the click event handler with the is method.

Categories
JavaScript Answers jQuery

How to clear a text area on focus with JavaScript?

Sometimes, we want to clear a text area on focus with JavaScript.

In this article, we’ll look at how to clear a text area on focus with JavaScript.

How to clear a text area on focus with JavaScript?

To clear a text area on focus with JavaScript, we can call the val method with an empty string when we add a focus event handler to the text area with focus.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<textarea>hello world</textarea>

to add a text area.

Then we write:

$('textarea').focus(function() {
   $(this).val('');
});

We select the text area with $.

Then we call focus to add a focus event handler with a function that empties the text area with $(this).val('');.

Therefore, when we click inside the text area, it becomes empty.

Conclusion

To clear a text area on focus with JavaScript, we can call the val method with an empty string when we add a focus event handler to the text area with focus.