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

Birthday Script Error

Explorer ,
Jan 07, 2017 Jan 07, 2017

Copy link to clipboard

Copied

I have the following script in a form, but I haven't dealt with JavaScript in a LONG time, and I'm stuck!  The process I want to happen is for the user to enter their birth date, and then the form displays a message indicating their age, then the field is validated and they move to the next field. I want the field to accept only "mm/dd/yyyy", but the user is allowed to enter just about anything they want, and the field accepts the entry. There is a simple error in the script causing the field not to catch incorrectly formatted field entries. What am I missing?

//Document Level Script:

function getAge(birthDate) {
    var today = new Date();
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }   
    return age;
}

//Custom Format Script:

if (!event.value) {
    event.value = "mm/dd/yyyy";
    event.target.display = display.noPrint
}
else {
    event.target.display = display.visible;
}

//Validation Script:

if(event.value != ""){
    event.target.required = false;
}
else {
    event.target.required = true;
}

if (event.value != "") {
var now = new Date(); // Todays Date
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);
var d = util.scand("mm/dd/yyyy", event.value); // Date of Birth entry by user

    var age = getAge(d);

if (d == null) {
  app.alert("Invalid date entry! Please enter your 'Date of Birth' in 'mm/dd/yyyy' format");
  event.rc = false;
}
    else if (age < 18) {
  app.alert("The date you entered indicates that you are less than 18 years of age!\n\nWe cannot process your application unless you are 18 years of age or older!", 1, 0)
        event.rc = false;
}
    else {
        app.alert("You have indicated that you are " + age + " years old.\n\nIf this is correct, please move to the next question.\n\nIf it is incorrect, check your date of birth and re-enter it as necessary.", 3, 0)

  event.rc = true; // this is redundant
  event.value = util.printd("mm/dd/yyyy", d); // strict format
}
}

event.target.required = (event.value == "" || !event.rc);

TOPICS
Acrobat SDK and JavaScript , Windows

Views

878

Translate

Translate

Report

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

Explorer , Jan 13, 2017 Jan 13, 2017

Good luck getting anyone in this place to give you more than a "hit and run" reply unless you plan to pay for services.

Once I had time to go over the script and investigate, I found that I placed the getAge function in the wrong location in the Validation script above.  I've included the corrected Validation script below, in case anyone wants to use it.  The function and format scripts listed above work correctly "as is" in my document.

//Validation Script:

if(event.value != ""){
    event.target.r

...

Votes

Translate

Translate
LEGEND ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

If a user enters a value like "12/10/1956"  is the date "December 10, 1956" or "October 12, 1956"?

You need to have a way to know exactly what the month and date value are or you cannot perform your task.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

If you enter 12/10/1956, it displays correctly "12/10/1956".  The calculation section works correctly, it's the entry validation process that has problems.  One could enter "1111" or "aB2CR3" and the field would accept the entry... if one did make such an entry, the calculation does not execute, it just jumps to the next field.  As long as one enters any date in "mm/dd/yyyy" format, the field works great with no errors.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 13, 2017 Jan 13, 2017

Copy link to clipboard

Copied

Good luck getting anyone in this place to give you more than a "hit and run" reply unless you plan to pay for services.

Once I had time to go over the script and investigate, I found that I placed the getAge function in the wrong location in the Validation script above.  I've included the corrected Validation script below, in case anyone wants to use it.  The function and format scripts listed above work correctly "as is" in my document.

//Validation Script:

if(event.value != ""){
    event.target.required = false;
}
else {
    event.target.required = true;
}

if (event.value != "") {
var now = new Date(); // Todays Date
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);
var d = util.scand("mm/dd/yyyy", event.value); // Date of Birth entry by user

if (d == null) {
  app.alert("Invalid date entry! Please enter your 'Date of Birth' in 'mm/dd/yyyy' format");
  event.rc = false;
}

    else {
var age = getAge(d);

    if (age < 18) {
  app.alert("The date you entered indicates that you are less than 18 years of age!\n\nWe cannot process your application unless you are 18 years of age or older!", 1, 0)
        event.rc = false;
}
    else {
        app.alert("You have indicated that you are " + age + " years old.\n\nIf this is correct, please move to the next question.\n\nIf it is incorrect, check your date of birth and re-enter it as necessary.", 3, 0)

  event.rc = true; // this is redundant
  event.value = util.printd("mm/dd/yyyy", d); // strict format
}
}
}
event.target.required = (event.value == "" || !event.rc);

Votes

Translate

Translate

Report

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 ,
Oct 27, 2022 Oct 27, 2022

Copy link to clipboard

Copied

LATEST

It works perfectly. 

I want to reset or clear the date of birth if it's < 18 after the alert.

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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