Validating numeric values in javascript

13-Jun-2017 04:35 by 4 Comments

Validating numeric values in javascript - dating nerdy women in ct

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.

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

The expressions use a pre-defined class is returned which enables the form to be submitted.

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.

Otherwise you can end up with values in the database that have no relation to reality.

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').

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

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.

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.

In Java Script to check for different month lengths, particularly for February in leap years, you need quite a bit of extra code.

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

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

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.