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

Script Code to validate a date

Community Beginner ,
Nov 04, 2021 Nov 04, 2021

I am trying to gat a section of my code to check to see if the day of a date is the first of the month (or "01").

I am sure the problem is at:  startDateString.getDate()==01  I just do not know how to properly write that part.

What i am trying to acomplish is: if the "StartDate" is blank OR if the day in the "StartDate" is the first (or "01) then it returns blank. if not, then it runs the "else" part of the code which works fine.

If the "StartDate" is blank then it returns blank which is good.

But its just not working when the "StartDate" is the first of the month.

This is code I am putting in a form field calculation if that helps

My code:

var startDateString = this.getField("StartDate").valueAsString;
var months =12;
var days = Number(this.getField("StartDate").valueAsString);
if (startDateString=="" || startDateString.getDate()==01) event.value = "";
else {var d = util.scand("mm.dd.yyyy", startDateString);d.setMonth(d.getMonth()+1); d.setDate(1);
event.value = util.printd("mm.dd.yyyy", d);}

 

TOPICS
General troubleshooting , JavaScript , PDF forms
1.6K
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 05, 2021 Nov 05, 2021

Thats because you are not entering same date format.

Use this:

var startDateString = this.getField("StartDate").valueAsString;
var x = startDateString.split("/");
if (startDateString=="" || x[1] == "01")
event.value = "";
else {
var d = util.scand("mm.dd.yyyy", startDateString);
d.setMonth(d.getMonth()+1);
event.value = util.printd("mm.dd.yyyy", d);}

View solution in original post

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
Community Expert ,
Nov 04, 2021 Nov 04, 2021

With util.scand convert startDateString to a date object. Then you can use getDate.

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
Community Beginner ,
Nov 04, 2021 Nov 04, 2021

Thanks Bernd, but I am not sure how to do that.

That code was modified from something else and I tried to edit it myself.

Maybe I should have started with the code I originally had without my modifications and expressed my question differantly. So, lets start here -- how do I add "or if the day of the month is the first" to the IF line in this (my original code) maybe starting here will be simplier...

 

var startDateString = this.getField("StartDate").valueAsString;

if (startDateString=="") event.value = "";

else {var d = util.scand("mm.dd.yyyy", startDateString);d.setMonth(d.getMonth()+1); d.setDate(1);

event.value = util.printd("mm.dd.yyyy", d);}

 

This works flawlessly, if "startDateString" is blank then it leaves it blank, or else if "startDateString" has any date in it then the else part of code runs perfect.

I just want to add an aditional instruction in the IF line to look for, which is: if the "startDateString" is blank OR if the day of "startDateString" (12/01/2021)happens to be the first of the month, then either of these will leave it blank.

I think maybe I had tried to do something that I wasnt sure of what I was doing in the original post.

 

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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

In the else part you can use d.getDate()

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
Community Expert ,
Nov 04, 2021 Nov 04, 2021

If format in "StartDate" field is also "mm.dd.yyyy" you can simply use 'slice' to check if it's "01". 

Something like this:

var startDateString = this.getField("StartDate").valueAsString;
var x = startDateString.slice(3,5);
if (startDateString=="" || x == "01")
event.value = "";
else {
var d = util.scand("mm.dd.yyyy", startDateString);
d.setMonth(d.getMonth()+1);
event.value = util.printd("mm.dd.yyyy", d);}

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
Explorer ,
Nov 04, 2021 Nov 04, 2021

I'm replying to this mainly because I can't figure out how to post a new question. Can someone help me with that?

 

As for this discussion...What language is the code that is discussed here? Does it run inside Adobe. In my career prior to retirement, I did a lot of coding in perl, visual basic...If memory serves (that's been 15 years ago), this code looks like those languages.

 

Thanks. Phillip

 

SweetTasha
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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

It's JavaScript, and yes, it runs in Acrobat.

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
Community Beginner ,
Nov 04, 2021 Nov 04, 2021

Thank you Nesa, but that didnt work.

When I entered any date like 3/01/2021 or 5/01/2021 in the "StartDate" field it is running the "ELSE" part of the code when it should be blank.

It should only run the else part of the code when the date is anythig other than the first of the month.

I copied the code as you have it. Should I have changed or replaced some part of it?

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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

Thats because you are not entering same date format.

Use this:

var startDateString = this.getField("StartDate").valueAsString;
var x = startDateString.split("/");
if (startDateString=="" || x[1] == "01")
event.value = "";
else {
var d = util.scand("mm.dd.yyyy", startDateString);
d.setMonth(d.getMonth()+1);
event.value = util.printd("mm.dd.yyyy", d);}

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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

I would do it using a Date object instead of a string, because if the user enters "1" instead of "01" your code won't work.

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
Community Beginner ,
Nov 05, 2021 Nov 05, 2021

This seems to be working, if user enters "01". If "1" is entered then it does not work which is what TRY67 is saying.

So, what is the syntax for date object? 

var startDateString = this.getField("StartDate").valueAsDateObject  ??

Or could I just add another OR object like:

if (startDateString=="" || x[1] == "01" || x[1] == "1")

Both fields are formatted the same: mm/dd/yyyy but user is entering "1". it does need to work both ways so no matter which the user enters

 

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
Community Beginner ,
Nov 05, 2021 Nov 05, 2021

Adding the extra OR argument does work:

if (startDateString=="" || x[1] == "01" || x[1] == "1")

But I would still be interested in knowing the other method that you stated TRY67.

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
Community Expert ,
Nov 05, 2021 Nov 05, 2021

My suggestion is to do it like this:

 

event.value = "";
var startDateString = this.getField("StartDate").valueAsString;
if (startDateString!="") {
	var d = util.scand("mm.dd.yyyy", startDateString);
	if (d!=null && d.getDate()!=1) {
		d.setMonth(d.getMonth()+1);
		event.value = util.printd("mm.dd.yyyy", d);
	}
}
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
Community Beginner ,
Nov 04, 2021 Nov 04, 2021

Can I add a varible that looks for the date something like:

var CheckDate=startDateString.getdate.value

and then:

if (startDateString=="" || CheckDate == "01")

event.value = "";

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
Community Beginner ,
Nov 05, 2021 Nov 05, 2021
LATEST

Thank you everyone for your help in this, especially you Nesa! I would not have been able to accomplish this by myself. We can close this thread if that is the protocol. but I may start another thread later with some more general questions on what the syntax agruments mean. I am the kind of person that once an answer is given I want to pick it apart to understand how it works. But that can be later. But I really appreciate the help!!

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