Skip to main content
Italian4215
Inspiring
February 5, 2016
Answered

Regular Expression help for limitting ranges

  • February 5, 2016
  • 1 reply
  • 2801 views

Hi, I am working on making a text field limited to dates, this is just a portion of the code. I already have validation of dates, but I am now trying to limit what the user enters by using a regular expression. This code works slightly however, it doesn't limit me for example I can enter in more than 2 digits but then it limits based on the total allowed so for example 8 digits are allowed if I just type. I need it to stop after the first 2 digits then have a - then 2 more digits then a - and then followed by 4 digits. I've tried limited each section and grouping as well. Any help would be greatly appreciated. Thanks.

This is in the format code and I am calling it in Keystroke.

function DateKS (){

var value = AFMergeChange(event);

if (!event.willCommit) {

        // Only allow characters that match the regular expression

        event.rc = /^([0]{0,1}[1-9]{0,1}|[1]{0,1}[012]{0,1})([-]{0,1})([0]{0,1}[1-9]{0,1}|[12]{0,1}[0-9]{0,1}|[3]{0,1}[01]{0,1})([-]{0,1})([0-9]{0,4})$/.test(value);

    }

}

This topic has been closed for replies.
Correct answer Italian4215

So you just gave up, finally?


I decided the check for 100 and 400 wasn't needed since that occurance only happens every 400 years. But I did work on it further and modified it even more. Here's my working code.

function isLeapYear(year) {
    return year % 4 === 0;
}

function checkDaysInMonth(month, day, year) {
    var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    if (month === 2) {
        if (isLeapYear(year)) {
            daysInMonth[1] += 1;
        }
    }

    return daysInMonth[month - 1] >= day;
}

function checkDateFormat(dateStr) {
    var errorMsg = '',
        maxYear = (new Date()).getFullYear(),
        minYear = maxYear - 1,
        matches = dateStr.match(/^(\d{2})-(\d{2})-(\d{4})$/),
        month,
        day,
        year;

    if (matches) {
        month = parseInt(matches[1], 10);
        day = parseInt(matches[2], 10);
        year = parseInt(matches[3], 10);

        if (month < 1 || month > 12) {
            errorMsg = 'Invalid value for month: ' + matches[1];
        } else if (day === 0) {
            errorMsg = 'Invalid value for day: ' + matches[2];
        } else if (!checkDaysInMonth(month, day, year)) {
            errorMsg = 'Invalid number of days for month: ' + matches[2];
        } else if (year < minYear || year > maxYear) {
            errorMsg = 'Invalid value for year: ' + matches[3] + ' - must be between ' + minYear + ' and ' + maxYear;
        }
    } else {
        errorMsg = 'Invalid date format: ' + dateStr + '\r\nPlease use format: mm-dd-yyyy';
    }

    return errorMsg;
}

function checkReceivedDate() {
    var value = AFMergeChange(event),
        errorMsg = '';
   
    // skip check if value is empty, since this field is not required
    if (!value) {
        return;
    }

    if (event.willCommit) {
        errorMsg = checkDateFormat(value);

        if (errorMsg) {
            app.alert(errorMsg, 0, 0, 'Error');
            event.value = '';

            return false;
        }
    } else {
        // Only allow characters that match the regular expression
        //event.rc = /^(?:0[1-9]?|1[012]?)?-?(?:0[1-9]?|[12][0-9]?|3[01]?)?-?2?0?[0-9]{0,2}$/.test(value);
        event.rc = /^\d{0,2}-?\d{0,2}-?\d{0,4}$/.test(value);
    }

    return true;
}

1 reply

Inspiring
February 5, 2016

I see nothing you code that inserting the separation character into the data being entered.

Life is much simpler and easier if you utilize the built-in features of Acrobat. I would work with setting the field format as needed and then using other built-in features as needed with other actions within the form.

Italian4215
Inspiring
February 5, 2016

can you give an example please. I am choosing to do it this way because I need my date field to be more strict than the standard built in one.

Here is all of my code:

function DateKS (){

var value = AFMergeChange(event);

if (!event.willCommit) {

        // Only allow characters that match the regular expression

        event.rc = /^([0]{0,1}[1-9]{0,1}|[1]{0,1}[012]{0,1})([-]{0,1})([0]{0,1}[1-9]{0,1}|[12]{0,1}[0-9]{0,1}|[3]{0,1}[01]{0,1})([-]{0,1})([0-9]{0,4})$/.test(value);

    }

}

function checkDate() {

    var minYear = 2015;

    var maxYear = (new Date()).getFullYear();

    var value = AFMergeChange(event);

    var errorMsg = "";

    // regular expression to match required date format

    var re = /^(\d{2})-(\d{2})-(\d{4})$/;

    if (value != '') {

      if (regs = value.match(re)) {

        if(regs[2] < 1 || regs[2] > 31) {

          errorMsg = "Invalid value for day: " + regs[2];

        } else if(regs[1] < 1 || regs[1] > 12) {

          errorMsg = "Invalid value for month: " + regs[1];

        } else if(regs[3] < minYear || regs[3] > maxYear) {

          errorMsg = "Invalid value for year: " + regs[3] + " - must be between " + minYear + " and " + maxYear;

        }

      } else {

        errorMsg = "Invalid date format: " + value + "\r\n" + "Please use format: mm-dd-yyyy";

      }

    }

    if(event.willCommit && errorMsg != "") {

      app.alert(errorMsg, 0, 0, "Error");

      this.getField('Received_Date').setFocus();

event.value = "";

      return false;

    }

    return true;

  }