Ensuring accuracy in form submissions for the month of February

I have a .net custom validator set up to validate an input date in a form. The validation should check the number of days in February, allowing 28/02/2015 but not 29/02/2015.

The C# server validation is functioning correctly with the following code snippet:

try
{
    DateTime dt = DateTime.Parse("29/02/2015");
}
catch
{
    args.IsValid = false;
}

This will return false when validated;

However, I am struggling to replicate the same behavior for client-side validation using JavaScript. Is there a straightforward way to achieve this in JavaScript?

Answer №1

Here's an updated version of my answer with a function that calculates the number of days in a given month and year:

function calculateDaysInMonth(month, year) {
   return new Date(year, month, 0).getDate();
}

//July
calculateDaysInMonth(7, 2009); //31
//February
calculateDaysInMonth(2, 2009); //28
calculateDaysInMonth(2, 2008); //29

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the information from the recently completed request

When the user inputs 'id' into the text field, I want to fetch a post based on the specific id entered by the user. If no id is provided, I would like to fetch the entire array of posts and then retrieve an id from the fetched data for testing pu ...

Is it necessary to include a module in another module if it is not utilized in the template?

Is it necessary to import Module2 into Module1 if a component from Module2 is being used in Module1, but only in the typescript and not the template? For instance, as a @ContentChild(Component2) component2 like shown below (Note: both modules are secondary ...

Could you provide me with the list of keywords associated with InstancedBufferGeometry attributes?

When attempting to change the position to position1 in the buffer geometry example provided here, it seems to not function properly. var geometry = new THREE.BufferGeometry(); // create a simple square shape. We duplicate the top left and bottom right // ...

Determine the possible width of a concealed element

I am currently customizing the lavalamp plugin to be compatible with dropdown menus, but I have come across a minor issue. I am trying to determine the offsetWidth of a hidden element. Obviously, this question is a bit nonsensical. What I actually need is ...

What Causes the Misalignment Between My Image and Text?

I am trying to randomly select a slide from the list of slides when the page loads, and then display it in the hero section of a website. However, I am facing an issue where the Image seems to be out of sync with the Text & Button (for example, displaying ...

Modify the main container width based on the size of the table

Would it be possible for the main container width to increase along with the table width? Is that achievable? <div class="container main-con"> <table class="table table-hover"> <tr><td></td></tr> </table> ...

"Webpack error: Including a comma within a string leads to a syntax problem when bund

I'm attempting to merge this file into my main JS. var constants = { height: 600, width: 400, default_bezier = "[ { \"startPoint\" : [51.6503017608354,203.464445873753], \"endPoint\" : [-52.41285540263849,202.372456432 ...

Retrieve data from MongoDB without the need for deserialization

I'm looking to efficiently retrieve a large amount of data from Mongo and send it directly to the client-side JavaScript without any additional processing in between. The goal is to avoid unnecessary serialization and deserialization steps. Although ...

Expression fragment in Thymeleaf

In splitting my templates into head/main/footer parts using thymeleaf, I have found a method to include stylesheets and javascript on certain pages while excluding them from others. This is achieved through the use of fragment expressions outlined here. M ...

Vue Dynamic Table Title

Is it possible to add labels to a pivot-table in Vue without affecting array indexes and drag-and-drop functionality as shown in the screenshot below? https://i.stack.imgur.com/5JTSM.png Are there alternative methods for implementing this feature? You c ...

Animate the transition effect as soon as the block is displayed

Looking to create a smooth page transition using CSS changes with the help of Javascript (jQuery). Initially, one of the pages is set to display none. page1 = $('.page1'); page2 = $('.page2'); page1.removeClass('animate').cs ...

The useEffect hook is failing to resolve a promise

I have received a response from an API that I need to display. Here is a snippet of the sample response (relevant fields only): [ { ...other fields, "latitude": "33.5682166", "longitude": "73 ...

When outputting the $http response in the console, Angular displays null instead of the expected result,

I have encountered a peculiar issue with my local webservice developed using Spring. Everything seems to be functioning correctly when accessing it through the browser or Postman, but for some reason, when attempting a simple GET method with Angular/Ionic, ...

Retrieving the data from an HTML button with JavaScript

Currently, I am working on creating a calculator. The HTML and CSS components are complete, but I am facing an issue with getting the button clicks to reflect in the display. As I am still new to JavaScript, I would appreciate any guidance on how to achiev ...

Accessing and fetching data from a PostgreSQL database using JavaScript through an API

I am currently working with an npm package called tcmb-doviz-kuru to fetch currency data from an API and then insert it into my database. However, I am facing an issue with mapping the data to properly insert the values. Below is my code snippet: var tcmbD ...

Repeated attempts to initiate ajax script failing to function

I am completely new to the world of Ajax, having just started learning about it a few days ago. Despite my lack of experience, I need to incorporate it into a form that I am creating for my employer. Unfortunately, I have been facing difficulties in getti ...

Utilizing JSON for sending model data to a controller in ASP.NET Core

I'm a beginner with ASP.NET Core and I'm attempting to send a view model to my controller. Data Model: public class ToolModel { public ToolModel() { } public string text { get; set; } public string type { get; set; } public stri ...

Iterate through each element in the array and manipulate the corresponding data

I am trying to figure out a way to assign a unique id to each item in an array and send it to the "script.js" file for individual data processing. <script href="script.js"></sciprt> script.js: $(function(){ $('#box' ...

Develop a custom autocomplete feature using Formik in React for selecting values from an array of objects

Looking to Add Autocomplete Functionality in Your React Application Using Formik for Seamless Selection of Single and Multiple Values from an Array of Objects? Take a Look at this Code snippet! [see image below] (https://i.stack.imgur.com/ekkAi.png) arra ...

`Weaving mesh into place with three.js`

I'm struggling to grasp how to position my cubes on the canvas. I can't quite figure out how positioning actually works. I'm trying to find a way to determine if my mesh reaches the limits of the canvas. But what exactly is the unit of posit ...