Custom date validation

The TMT Validator has built-in pattern for 14 different date formats. To create a new custom date format we will take advantage of the Validator's extensibility.

Rules for date validation are stored inside tmt.validator.datePatterns, a JavaScript object, acting as a container for both built-in and custom date validation patterns. In order to add our own, we can call the tmt.validator.createDatePattern function.

The tmt.validator.createDatePattern() function requires 5 arguments:

  1. Regular expression that will validate date formatting
  2. Position (zero based) of the years inside our date format
  3. Position (zero based) of the months inside our date format
  4. Position (zero based) of the days inside our date format
  5. String used to separate between days/months/years

The resulting JavaScript code is:

<script type="text/javascript">
tmt.validator.datePatterns["spacedate"] = tmt.validator.createDatePattern("^\([0-3]?[0-9]\)\\s\([0-2]?[0-9]\)\\s\([0-9]{4}\)$", 2, 1, 0, " ");
</script>

The XHTML code for the field looks like this:

<input type="text" name="mydate" tmt:datepattern="spacedate" />

Custom date validation
Custom date (required, date, D M YYYY)