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

Date Validation Bug Assistance

New Here ,
Dec 23, 2016 Dec 23, 2016

Hi,

I've searched the forums "date validation" topics but haven't seen this particular issue discussed. Sorry in advance if it's redundant.

I have two fields: "Month Ended" and Due_Date." Month Ended is user input with "date validation." Due_Date is calculated based on the user input in Month Ended.

The format for Month Ended and Due_Date is set to validate for mm/dd/yyyy.

Here are three scenarios and results:

1. User enters 9/30/2016. Due Date updates to 10/31/2016.

2. User enters 9/30/16. Validation script catches and prompts them to re-enter.

3. User enters 9/30. Month Ended "auto-corrects" to 9/30/2016 on the screen. But then Due Date adjusts to 01/00/0000.

Scenario 3 is what I need help resolving.

Below is the code in each text box.

MONTH ENDED DATE VALIDATION BOX (Note: This is also in the Date Validation box for Due_Date)

// Validates that the input string is a valid date formatted as "mm/dd/yyyy"

function isValidDate(dateString)

{

    //first check for the pattern

    if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))

        return false;

    //parse the date parts to integers

    var parts = dateString.split("/");

    var day = parseInt(parts[1], 10);

    var month = parseInt(parts[0], 10);

    var year = parseInt(parts[2], 10);

    //check the ranges of month and year

    if(year < 1000 || year > 3000 || month == 0 || month > 12)

        return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    //adjust for leap years

    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))

        monthLength[1] = 29;

    //check the range of the day

    return day > 0 && day <= monthLength[month - 1];

};

DUE_DATE CUSTOM CALCULATION SCRIPT BOX

//set variables and populate

var monthEnded = new Date(this.getField("Month Ended").value);

var currentYear = monthEnded.getFullYear();

var currentMonth = monthEnded.getMonth();

var lastDayofNextMonth = new Date(currentYear, currentMonth+2, 0);

event.value = util.printd("mm/dd/yyyy", lastDayofNextMonth);

TOPICS
Acrobat SDK and JavaScript
985
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

LEGEND , Dec 27, 2016 Dec 27, 2016

Acrobat JavaScript has a number of methods for dealing with dates that Web JavaScript does not. Have you checked to see if your statement

var monthEnded = new Date(this.getField("Month Ended").value);

actually creates a valid date object?

You script corrected with some debugging informational information showing on the Acrobat JavaScript console:

//set variables and populate

console.show();

console.clear()

console.println("\NInputted Month ;End string: " + this.getField("Month Ended").value)

console.pri

...
Translate
LEGEND ,
Dec 27, 2016 Dec 27, 2016

Acrobat JavaScript has a number of methods for dealing with dates that Web JavaScript does not. Have you checked to see if your statement

var monthEnded = new Date(this.getField("Month Ended").value);

actually creates a valid date object?

You script corrected with some debugging informational information showing on the Acrobat JavaScript console:

//set variables and populate

console.show();

console.clear()

console.println("\NInputted Month ;End string: " + this.getField("Month Ended").value)

console.println("\Nnew Date(this.getField(\"Month Ended\").value): " + new Date(this.getField("Month Ended").value));

console.println("\Nutil.scand(\"mm/dd/yyyy\", this.getField(\"Month Ended\").value));: " +util.scand("mm/dd/yyyy", this.getField("Month Ended").value));

var monthEnded = util.scand("mm/dd/yyyy", this.getField("Month Ended").value);

var currentYear = monthEnded.getFullYear();

var currentMonth = monthEnded.getMonth();

var lastDayofNextMonth = new Date(currentYear, currentMonth + 2, 0);

console.println("\nLastDayofNextMonth: " + lastDayofNextMonth);

event.value = util.printd("mm/dd/yyyy", lastDayofNextMonth);

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
New Here ,
Jan 10, 2017 Jan 10, 2017

Sorry for the delayed response. I've been out. Thank you for your help on this!

I'm working through an issue this solution created. If I enter "9/30" in the "Month Ended" text box, it auto corrects both the "Month Ended" and "Due Date" boxes, but to 2017 dates. I'm assuming that's because we are in 2017 now. I'm not sure how you could "divine" that the user meant "9/30/2016" instead of 9/30/2017 though...maybe by comparing the "Month Ended" to "Today's Date" and prompting a dialogue box if "Month Ended" was after today's date asking the user whether that was the date they wanted to enter...

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
New Here ,
Jan 10, 2017 Jan 10, 2017
LATEST

Here is what I have pop up to deal with the issue discussed above. Probably not the best solution but hopefully it will prompt people to correct the date if it wasn't what they wanted. As a side note, I also have the "dateToday" field in the form which is why I just call it here. Just wanted to share in case others had similar problems.

var MonthEnded = getField("Month Ended").valueAsString;

var Today = getField("dateToday").valueAsString;

var d1 = util.scand("mm/dd/yyyy",MonthEnded);

var d2 = util.scand("mm/dd/yyyy",Today);

if(d1>d2)

{

app.alert({cMsg:"The month you are completing the form for is after today's date. Please adjust if not correct.\n\n" + "Remember the date format must be entered as 'mm/dd/yyyy'.", nIcon:1});

}

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 ,
Dec 28, 2016 Dec 28, 2016

"/Your validation script will work only if the user enters the date with the "/" delimiter. Unfortunately or fortunately the acceptable separators for dates allow the ".". "-",  and " " (space") characters. The exact value of the field will be the user entered value and not the display format. You can modify your RegExp string to accept these other characters. But you might also want to consider using the "util.scand" method of Acrobat JavaScript that will check the entered date string as being a valid date or not. One can then use the various JavaScript date object methods to determine if the date entered is the last day of the month for the date entered.

When using the custom validation you might want to include a message when the validation fails to explain the error and how to correct it. Also one can set the Acrobat JavaScript event return code property to single to the JavaScript processes that the validation failed.

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