84283 days on xHamster
73641M profile views
15974K subscribers
4270 comments left

Validating date javascript

Description: This script validates a date field to ensure it's in the format mm/dd/yyyy.

It also intelligently checks that the date ranges are valid, so something like 02/30/2005 is caught.

The output will be almost identical to the earlier examples.

In this simple example we can even rewrite the In practice most modern browsers will now use HTML form validation to preempt any Java Script validation - with the notable exception of Safari. Visitors from some countries may find it confusing that we're using the and vice-versa as the year and day values have now changed position.

Each of these values is then tested against an allowed range (days: 1 - 31; months: 1 - 12; years: 1902 - 2018; and so on).

This script only confirms that the input format is correct and that each individual value falls within its allowed range.

When you enter a date in the format dd/mm/yyyy the value is sent via an Ajax call to the server where it is validated using the PHP checkdate function.

For example, when the date is checked, the return value, , is an array with elements 1 through 3 containing the day, month and year components of the input string.

For the time check, the array returned includes the hour (pos 1), minutes (pos 2) and, optionally, the am/pm string (pos 3).

In this example, the date fields will only accept input that matches the pattern 'dd/mm/yyyy' (this could just as easily be changed to 'yyyy-mm-dd' or 'mm/dd/yyyy').

The time field will allow input starting with 'hh:mm' following by an optional 'am' or 'pm'. The code behind the form is as follows: For each field in the form (first the dates, then the time field), a check is made as to whether the input is blank.

When capturing information for insertion into a database, or use in other processing, it's important to control what the user can enter.

Please or register to post comments
If spammers comment on your content, only you can see and manage such comments Delete all
Date Validation Cross Browser Date Validation in JS - mm/dd/yyyy format. JS Date validation code, isDate Function. 
08-Feb-2019 07:20
I think Niklas has the right answer to your problem. Besides that, I think the following date validation function is a little bit easier to read // Validates that the input string is a valid date formatted as "mm/dd/yyyy" function isValidDatedateString { // First check for the pattern if!/^\d{1,2}\/\d{1,2}\/\d{4}$/.testdateString return false;. 
08-Feb-2019 07:23
JavaScript ISO Dates. ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax YYYY-MM-DD is also the preferred JavaScript date format. 
08-Feb-2019 07:26

Validating date javascript introduction

Validating date javascript