skip to content »

ik-kem.ru

Validating date using javascript

It does not check for leap years or invalid dates at the end of short months.

function Validate Form(ctrl) //-------------------------------------------------------------------------- //This function verifies if the start date is prior to end date.If the valid date character is changed please make sure you replace it in the HTML and Javascript code too.Now the date string is seperated into the month, day and year integers.//Begin function checkdate function checkdate(input) //Begin function button1_onclick //This makes sure that even if it somehow got past the original error check, //the report won't run unless the date format is correct.function button1_onclick(ctrl) //End function button1_onclick //This goes in the BODY portion of the HTML Add this phrase to any date control you want to call the function from: on Change="checkdate(this)" //We had to use onchange instead of onblur //because our call center software was interfering //with HTML code and causing the control to lose focus - // we ended up in an infinite loop of error messages.The code behind the form now is as follows: If you're not already familiar with regular expressions, then this might be getting a bit complicated.

Basically, for each of the regular expression tests, an array is returned holding each component of the pattern that we've matched.

This routine DOES NOT check that the date or time input values are valid, just that they match the required format (d/m/y and h:m). Once you're in control of the input format, it's a lot easier to check that the values are actually valid.

The function has been improved now so that the day, month and year values are checked to ensure that they're in the right ball-bark (ie. Also the year must be between 1902 and the current year.

The following example shows how you can do this for the mm/dd/yyyy format.

We have also provided the javascript date validation for the dd/mm/yyyy format.

If not, the input is compared to the regular expression.