Date validation based on dd/mm/yyy

Community Beginner ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Hi all,

 

I have a date field where I need users to insert the exact date in format dd/mm/yyyy. I configured a custom Date field with the above format, but it seems if the users insert other formats like dd/mm, these formats are accepted and the current year is automatically displayed, while it's not assigned in the underlying field (please refer to the attachment).

 

I thought to configure a validation to ensure date is inserted with all its parts, and I used the following validation script which seems not working:

 

var myDate = event.target.valueAsString;
var DD = myDate.substr(0,2);
var MM = myDate.substr(3,2);
var YYYY = myDate.substr(6,2);
if ( (DD > 31 || DD < 1) || (MM > 12 || MM < 1) || (myDate.length != 10) || (myDate.substr(2,1) != "/") || (myDate.substr(5,1) != "/") )
{app.alert({ cMsg: "Please insert the full date in format dd/mm/yyyy", cTitle: "" });
event.rc = false;}

. Any help is really appreciated.

 

Thanks

Simone

 

TOPICS
JavaScript, PDF forms

Views

63

Likes

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

Most Valuable Participant , Apr 23, 2021 Apr 23, 2021
I would check first that the input has the desired format (using a Regular Expression) and then that it's actually a valid date, by converting it to a Date object. Try it like this: if (event.value) { var d = util.scand("dd/mm/yyyy", event.value); if (/^\d{2}\/\d{2}\/\d{4}$/.test(event.value)==false || d==null) { app.alert("Invalid date string entered. You must enter a date in the following pattern: dd/mm/yyyy"); event.rc = false; } }

Likes

Translate

Translate
Most Valuable Participant ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

I would check first that the input has the desired format (using a Regular Expression) and then that it's actually a valid date, by converting it to a Date object. Try it like this:

 

if (event.value) {
	var d = util.scand("dd/mm/yyyy", event.value);
	if (/^\d{2}\/\d{2}\/\d{4}$/.test(event.value)==false || d==null) {
		app.alert("Invalid date string entered. You must enter a date in the following pattern: dd/mm/yyyy");
		event.rc = false;
	}
}

 

Likes

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 Beginner ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

LATEST

It works, thank you!

Likes

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