Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Regular Expression help for limitting ranges

Participant ,
Feb 05, 2016 Feb 05, 2016

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);

    }

}

TOPICS
Acrobat SDK and JavaScript
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Feb 09, 2016 Feb 09, 2016

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

...
Translate
Community Expert ,
Feb 08, 2016 Feb 08, 2016

So you just gave up, finally?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 09, 2016 Feb 09, 2016
LATEST

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;
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 08, 2016 Feb 08, 2016

I would take advantage of the built-in features of Acrobat to validate the date input and then use JavaScript to change the entered value either by replacing the separators with the "-" by any number of ways. You can use the RegExp object to split the value into the month, day, and year elements and then combine them with the "-" separator or use the "util.scand" and "uti.printf" methods to reformat the entered data with the "-" separator. One could even us the "split" method the "substr" method.

I guess I could post a sample form utilizing all these approaches if needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 08, 2016 Feb 08, 2016


If you could please that would be great, but nonetheless, I would still like to get it working the way I am doing it. I'm very close to having it operate 100% correct, just need that last part that I posted fixed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 08, 2016 Feb 08, 2016

What you are seeing on the form field is the formatted displayed value but if you try to chang the entered value you see the actual value that was entered not the formaated value. You need to add a script to convert the actual value of the field to formatted value. I would look at converting the inputed valuel in a date object and then setting the field's value formatted with the specific format using the "util.printd()" method. This way you let the form validate the date values you enforce the strict formatting of the field value.

This is why the form field displays a specific format but the value of the field has a different format or even a different value. This feature makes a lot of other tasks much easier like dealing with percentages and displaying the formatted value with the decimal adjustment and the "%" but keeping the decimal value or displaying an elapsed time has "hours:minutes" but keeping the value as the elapsed minutes which can easily be summed without additional conversion to minutes only.

I would use a custom validation script like:

if(event.value != null) {

// have value force format to mm-dd-yyyy;

event.value = util.printd("mm-dd-yyyy", util.scand("mm-dd-yyyy", event.value));

}

You then can use the custom format  of "mm-dd-yyyy" for the Date format. The user can enter separators other than "-" but the value of the field will be forced to use "-" and you verify this use JavaScript in a later field to retrieve the field's value or go back to the field and see what the value now shows.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines